Facebook
From Adjowa Asamoah, 1 Month ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 139
  1. void setup() {
  2.   size(400, 400);
  3.   background(255);
  4.  
  5.   int numCells = 10; // Number of cells in each direction
  6.   int cellSize = width / numCells; // Size of each cell
  7.  
  8.   // Draw grid of rectangles
  9.   for (int i = 0; i < numCells; i++) {
  10.     for (int j = 0; j < numCells; j++) {
  11.       int x = i * cellSize;
  12.       int y = j * cellSize;
  13.       rect(x, y, cellSize, cellSize);
  14.     }
  15.   }
  16.  
  17.   // Draw x-axis
  18.   stroke(0);
  19.   line(0, height / 2, width, height / 2);
  20.  
  21.   // Draw y-axis
  22.   line(width / 2, 0, width / 2, height);
  23.  
  24.   // Plot and connect points
  25.   float[] xValues = {0, 1, 2, 3, 4, 5};
  26.   float[] yValues = {1, 4, 2, 6, 3, 5};
  27.  
  28.   for (int i = 0; i < xValues.length; i++) {
  29.     // Map points to the grid
  30.     float mappedX = map(xValues[i], 0, 5, 0, width);
  31.     float mappedY = map(yValues[i], 0, 6, height, 0);
  32.    
  33.     // Plot points
  34.     fill(255, 0, 0); // Red color
  35.     ellipse(mappedX, mappedY, 10, 10);
  36.    
  37.     // Connect points with lines
  38.     if (i > 0) {
  39.       float prevMappedX = map(xValues[i-1], 0, 5, 0, width);
  40.       float prevMappedY = map(yValues[i-1], 0, 6, height, 0);
  41.       line(prevMappedX, prevMappedY, mappedX, mappedY);
  42.     }
  43.   }
  44. }
  45.