极坐标系和笛卡儿坐标系

2022-04-29  本文已影响0人  大龙10

书名:代码本色:用编程模拟自然系统
作者:Daniel Shiffman
译者:周晗彬
ISBN:978-7-115-36947-5
目录

3.5 极坐标系和笛卡儿坐标系

1、笛卡儿坐标系和极坐标系

2、向量的两种表示方法

3、极坐标的应用

  在某些应用程序中,这样的坐标转换是非常有用的。
  比如,让一个图形绕着圆运动,我们用笛卡儿坐标系很难实现这样的功能。用极坐标系却很容易实现,转动角度就可以了。

4、示例

示例代码3-4 将极坐标转化为笛卡儿坐标

float r;
float theta;

void setup() {
  size(400, 400);
  // Initialize all values
  r = height * 0.45;
  theta = 0;
}

void draw() {
  
  background(255);
    
  // Translate the origin point to the center of the screen
  translate(width/2, height/2);
  
  // Convert polar to cartesian
  float x = r * cos(theta);
  float y = r * sin(theta);
  
  // Draw the ellipse at the cartesian coordinate
  ellipseMode(CENTER);
  fill(0,0,127);
  stroke(0);
  strokeWeight(2);
  line(0,0,x,y);
  ellipse(x, y, 45, 45);
  
  // Increase the angle over time
  theta += 0.02;
}

5、运行结果

将极坐标转化为笛卡儿坐标
上一篇 下一篇

猜你喜欢

热点阅读