简体   繁体   English

在处理中从csv文件中绘制多个形状

[英]Draw multiple shapes from csv file in processing

I'm new to Processing, and I want to make X number of ellipses.我是 Processing 的新手,我想制作 X 个椭圆。 I'll get that number from a csv file.我将从 csv 文件中获取该数字。 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.我将从 csv 文件中获取该数字。

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. ?)您是从 csv 文件中读取每个椭圆的坐标(行数是绘制的椭圆数)还是只是读取一个整数值X ,它代表您需要绘制的椭圆数(无论它们的位置/大小如何) /等等。 ?)

Let's start with the most basic part: drawing ellipses.让我们从最基本的部分开始:绘制椭圆。 This is achieved using the ellipse() function which requires 4 parameters:这是使用ellipse()函数实现的,该函数需要 4 个参数:

  1. x position x 位置
  2. y position y 位置
  3. width宽度
  4. height高度

eg drawing a 20x20 pixels ellipse at coordinates 10,10:例如在坐标 10,10 处绘制一个 20x20 像素的椭圆:

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.在读取 CSV 文件方面,正如我在评论中提到的, loadTable()函数将对此有所帮助。 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.以下是如何根据 csv 文件中存在的 x,y 值绘制椭圆的基本示例。 It loads a csv file, then loops through each row.它加载一个 csv 文件,然后循环遍历每一行。 For each row it accesses the first and second value.对于每一行,它访问第一个和第二个值。 Notice the indexing starts at 0, not at 1.请注意,索引从 0 开始,而不是从 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:这是示例 data.csv 文件:

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.你可以用Table 类做很多花哨的事情,比如添加标题,这将允许你通过标签而不是索引来获取值。

If you're simply reading a value X and you draw ellipses based on different parameters, you simply need to use a for loop .如果您只是读取值 X 并根据不同的参数绘制椭圆,则只需使用for 循环 If you used programming basics like this, they're easy as 1,2,3:如果你使用这样的编程基础,它们就像 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 数到 B。假设您正在数 10 步。 A for loop will have 3 requirements: for 循环有 3 个要求:

  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 ; ):语法大致如下(3 个要求由;分隔):

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):此时可以跳过步骤(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:这可以很容易地应用于绘制 x 个椭圆:

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 :听起来您正在寻找一个基本的for 循环

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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