简体   繁体   中英

Draw multiple shapes from csv file in processing

I'm new to Processing, and I want to make X number of ellipses. I'll get that number from a csv file. Is there any option to make multiple ellipses?

Your question is a bit ambiguous, especially this part:

I'll get that number from a csv file.

Are you reading the coordinates of each ellipse from a csv file (the number of rows being the number of ellipses drawn) or you simply reading a single integer value X which represents the number of ellipses you need to draw (regardless of their position/size/etc. ?)

Let's start with the most basic part: drawing ellipses. This is achieved using the ellipse() function which requires 4 parameters:

  1. x position
  2. y position
  3. width
  4. height

eg drawing a 20x20 pixels ellipse at coordinates 10,10:

ellipse(10,10,20,20);

In terms of reading a CSV file, as I've mentioned in the comment, the loadTable() function will help with that. The reference page actually includes an example of how to traverse each row and extract values.

Here is a basic example of how to draw ellipses based on x,y values present in a csv file. It loads a csv file, then loops through each row. For each row it accesses the first and second value. Notice the indexing starts at 0, not at 1.

Table table;

void setup(){
  //load the csv file
  table = loadTable("data.csv");
  //loop through each row
  for (TableRow row : table.rows()) {
    //extract the first and second value from the current row
    float x = row.getFloat(0);//extract the first value on the row, at index 0
    float y = row.getFloat(1);//extract the second value on the row, at index 1
    //use the current x,y values to draw an ellipse
    ellipse(x,y,5,5);
  }
}

and here is the sample data.csv file:

10,10
20,20
50,50
90,90

示例文件:从 csv 数据中绘制的椭圆

You can do quite a few fancy things with the Table class like adding headers, which will allow you to get the values by their labels, rather than by index.

If you're simply reading a value X and you draw ellipses based on different parameters, you simply need to use a for loop . If you used programming basics like this, they're easy as 1,2,3:

  1. declaring and initializing a variable
  2. using a condition
  3. accessing and modifying an existing variable

You can imagine them as a code structure that allows you to count from A to B. Let's say you're counting 10 steps. A for loop will have 3 requirements:

  1. a number with an initial value
  2. a condition (to know when to stop)
  3. an increment: how fast is the number changing from the initial to the final value

The syntax roughly looks like this (with the 3 requirements separated by ; ):

for (initial value ; condition ; incrementation){
   //instructions to repeat while condition is true
}

For example:

for (int step = 0; step < 10; step = step+1) {
  println("step " + step);
}

At this point you can skip steps (hop):

for (int step = 0; step < 10; step = step+2) {
  println("step " + step);
}

or even walk/count backwards:

for (int step = 10; step > 0; step = step-1) {
  println("step " + step);
}

This then can be easily applied to drawing x number of ellipses:

int x = 30;//assuming this value can be easily read
for (int ellipseCount = 0; ellipseCount < x; ellipseCount = ellipseCount+1) {
  float size = random(10);
  ellipse(random(width),random(height),size,size);
}

It sounds like you're looking for a basic for loop :

int ellipseCount = 10; //get this from csv file
size(500, 500);
background(0);
for (int i = 0; i < ellipseCount; i++) {
  ellipse(random(width), random(height), 10, 10);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM