If you want to “roll your own” and not use arc()
, then the formula is not to hard for circle coordinates – radius*cos(a)
and radius*sin(a)
for a point at angle a
.
To draw line segments, loop over the points and draw a line between the previous point and the next.
void myarc(float radius, float arcsize, float step) {
line(0, 0, radius, 0);
for (float angle=step; angle<=arcsize; angle+=step) {
float x1 = radius*cos(angle-step);
float y1 = radius*sin(angle-step);
float x2 = radius*cos(angle);
float y2 = radius*sin(angle);
line(x1, y1, x2, y2);
if (angle+step>arcsize) line(0, 0, x2, y2);
}
}
This also lets you get more creative, for example, varying line thickness or color, or drawing shapes at the points.
/**
* create your own simple arc drawing function
* ( rather than using the built-in `arc()` )
* 2020-09-16 Processing 3.5.4
*/
void setup() {
size(900, 300);
strokeWeight(2);
}
void draw() {
background(255);
translate(width/6.0, height/2.0);
myarc(140, TWO_PI*0.75, 0.025);
translate(width/3.0, 0);
myarc(140, TWO_PI*(mouseX/(float)width), 0.025);
translate(width/3.0, 0);
myarc2(140, TWO_PI*(mouseX/(float)width), 0.1);
}
void myarc(float radius, float arcsize, float step) {
line(0, 0, radius, 0);
for (float angle=step; angle<=arcsize; angle+=step) {
float x1 = radius*cos(angle-step);
float y1 = radius*sin(angle-step);
float x2 = radius*cos(angle);
float y2 = radius*sin(angle);
line(x1, y1, x2, y2);
if (angle+step>arcsize) line(0, 0, x2, y2);
}
}
void myarc2(float radius, float arcsize, float step) {
for (float angle=step; angle<=arcsize; angle+=step) {
float x1 = radius*cos(angle);
float y1 = radius*sin(angle);
ellipse(x1, y1, 10, 10);
}
}