简体   繁体   English

P5.js用矩形绘制圆形

[英]P5.js draw circle shape with rectangles

I'm trying to form a circle shape from the outline of several rectangles in P5.js 我正在尝试从P5.js中几个矩形的轮廓形成一个圆形

This is what I have so far but as you can see it's not quite a circle. 这是我到目前为止所拥有的,但是正如您所看到的,这还不是一个小圆圈。
https://codepen.io/anon/pen/KZaOKB https://codepen.io/anon/pen/KZaOKB

const width = 400;
function setup() { 
  createCanvas(width, width);
} 
function draw() { 
  background(220);
  noStroke();
  fill(color(175,100,220));

  for (var i = 0; i <= 36; i++) {
    var e = radians(i * 10);
    var height = 150 * sin(e/2) * 2;

    rect(i*11 ,(width/2)-10-(height/2), 10, height);
  }
}

This is the formulas I'm using to find a circles chord. 这是我用来查找和弦的公式。
在此处输入图片说明

I'm not sure if there is something wrong with my maths or I'm using the completely wrong formula. 我不确定数学是否有问题或使用的公式完全错误。 Thanks 谢谢

This is called an arcs Sagitta (The height of an arc or segment) 这称为弧线弧线(弧线或线段的高度)

I had the Radius and Sagitta and needed to calculate the arcs Width (aka. the length of the chord) using the following formula: 我有Radius和Sagitta,需要使用以下公式计算弧度Width(也就是弦的长度):

l = √2sr−s2

s: is the length of the sagitta s:箭矢的长度
r: is the radius of the arc r:圆弧的半径
l: is one half of distance across the base of the arc (half the chord length) l:是圆弧底部距离的一半(和弦长度的一半)
Note In all the above formulae, the length l is half the width of the arc. 注意在所有上述公式中,长度l是弧的宽度的一半。 The full width will be double this. 全宽将是此宽度的两倍。 Simply multiply l by 2 只需将l乘以2

圆弧

JavaScript code using P5.js 使用P5.js的JavaScript代码

 const r = 200; const lineWidth = 10; const lines = (r * 2) / lineWidth; function setup() { createCanvas(400, 400); } function draw() { background(220); noStroke(); fill(color(175,100,220)); for (var i = 0; i <= lines; i++) { const s = (i * lineWidth) + lineWidth; const chordLength = (Math.sqrt((2 * s * r) - (s*s)) * 2); rect(i * lineWidth, r - (chordLength / 2), lineWidth-1, chordLength); // rect(x, y, width, height) } } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.11/p5.min.js"></script> 

I wrote you a function for doing this: 我为您编写了一个用于执行此操作的函数:

This is p5.js: 这是p5.js:

function drawCircle(x, y, radius) {
  for (var i = 0; i <= 360; i++) {
    rect(x, y, cos(i) * radius, sin(i) * radius);
  }
}

This is Processing: 正在处理:

void drawCircle(float x, float y, float radius) {
  for (int i = 0; i <= 360; i++) {
    rect(x, y, cos(i) * radius, sin(i) * radius);
  }
}

'i' is the angle (were doing this 360 times. if you want finer detail or you are drawing really big corcles you may want to go smaller steps. eg: i += 0.1 this will however increase the time needed to caluclate this) 'x' and 'y' is the position and radius is the circle radius. “ i”是角度(执行此操作360次。如果您想要更精细的细节,或者正在绘制很大的皮质,则可能需要走更小的步长。例如: i += 0.1但这会增加计算该角度所需的时间) “ x”和“ y”是位置,半径是圆半径。

You call the function like this: 您可以这样调用函数:

P5.js: P5.js:

function setup() {
   createCanvas(100, 100);
   background(0);
   drawCircle(width / 2, height / 2, 50);
}

Processing: 处理:

void setup () {
  size(100, 100);
  background(0);
  drawCircle(width / 2, height / 2, 60);
}

This is what the circle looks like (who guessed - i looks like a circle): 这是圆圈的样子(谁猜到了-我看起来像一个圆圈):

圆

If we visualize the rectangles: 如果我们可视化矩形:

圆可视化

My totally professional formulas: 我完全专业的公式:

公式

I hope this could help. 我希望这会有所帮助。 Have a nice day! 祝你今天愉快! :D :d

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

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