提问人:Muhammad Anas Sajjad 提问时间:10/22/2023 最后编辑:Muhammad Anas Sajjad 更新时间:10/24/2023 访问量:57
具有特定填充百分比的圆
A circle that has a specific percentage amount of fill
问:
我想创建一个填充到一定百分比的圆圈,如下所示:
我正在使用处理 Java。
我的尝试是使用 arc()
函数,但是我很难确定应该如何计算 和 的值:angle1
angle2
percentageRemaining = 0.25;
angle1 = ????
angle2 = ????
noStroke();
fill(35);
arc(100, 100, 200, 200, angle1, angle2, CHORD);
到目前为止,我确实尝试了一些修复程序,但没有运气。
答:
2赞
Sash Sinha
10/23/2023
#1
我的计算机上没有 Processing,所以我尝试使用 editor.p5js.org(Javascript 版本的 Processing)。转换应该很容易:
let startTime;
let circleRadius = 200;
function setup() {
createCanvas(800, 600);
startTime = millis();
}
function draw() {
background(255);
let elapsedTime = (millis() - startTime) / 500.0;
let percentage = min(elapsedTime / 10.0, 1);
let fillHeight = circleRadius * 2 * percentage;
// Draw blue rectangle fill, from bottom to top
fill(0, 0, 255);
noStroke();
rect(width / 2 - circleRadius, height / 2 + circleRadius - fillHeight, circleRadius * 2, fillHeight);
// White overlay to mask outside of circle
fill(255);
noStroke();
beginShape();
for (let angle = 0; angle < TWO_PI; angle += 0.01) {
let x = width / 2 + circleRadius * cos(angle);
let y = height / 2 + circleRadius * sin(angle);
vertex(x, y);
}
for (let angle = TWO_PI; angle > 0; angle -= 0.01) {
let x = width / 2 + (circleRadius + 100) * cos(angle);
let y = height / 2 + (circleRadius + 100) * sin(angle);
vertex(x, y);
}
endShape(CLOSE);
// Draw outer circle
noFill();
stroke(0);
strokeWeight(5);
ellipse(width / 2, height / 2, circleRadius * 2);
// Draw percentage text
let textY = height / 2;
let percentText = nf(percentage * 100, 0, 2) + "%";
fill(textY < height / 2 + circleRadius - fillHeight ? 0 : 255);
noStroke(); // Remove outline from text
textSize(32);
textAlign(CENTER, CENTER);
text(percentText, width / 2, textY);
}
输出:
不完美...但应该是一个合理的开始!
上一个:在加工过程中打印菱形
评论
let startTime
int startTime; and
int circleRadius
function
void
mask()
PGraphics