Processing中线条未显示求助:需绘制圆心间连线
求助:绘制中心圆到外侧圆的连线失败
我试了好几种办法都没用,求大家帮忙。我需要画出从中心圆圆心到各个外侧圆圆心的连线,以下是我的代码实现:
原代码
void drawTree(float x, float y, float scale, float bulbs, float radius) { for (float i = 0.0; i<TWO_PI; i+=TWO_PI/bulbs) { fill(#13430C); noStroke(); circle(x+cos(i)*scale, y+sin(i)*scale, radius); line(x, y, x+cos(i)*scale, y+sin(i)*scale); } fill(#5D4A07); noStroke(); circle(x, y, radius); }
调用方法:
void draw(){ drawTree(width/2, height/2, 60, 5, 50); }
问题分析与修复方案
你的代码有两个核心问题导致连线无法正常显示:
noStroke()禁用了线条绘制:绘制外侧圆时调用的noStroke()会作用于后续的line(),导致线条没有描边,根本无法显示。- 中心圆覆盖了连线:中心圆最后绘制,会挡住连线靠近中心的部分。
调整后的代码:
void drawTree(float x, float y, float scale, float bulbs, float radius) { // 先画中心圆,避免遮挡后续连线 fill(#5D4A07); noStroke(); circle(x, y, radius); // 开启描边,设置线条样式(颜色、粗细可按需调整) stroke(#000000); strokeWeight(2); for (float i = 0.0; i < TWO_PI; i += TWO_PI/bulbs) { float outerX = x + cos(i)*scale; float outerY = y + sin(i)*scale; // 绘制外侧圆,临时关闭描边 fill(#13430C); noStroke(); circle(outerX, outerY, radius); // 恢复描边,绘制中心到外侧圆的连线 stroke(#000000); strokeWeight(2); line(x, y, outerX, outerY); } } void draw(){ background(255); // 添加上背景色,防止重复绘制叠加 drawTree(width/2, height/2, 60, 5, 50); }
关键调整点
- 调换绘制顺序:先画中心圆,让连线在中心圆上方完整显示
- 按需控制
stroke():绘制圆时关闭描边,绘制线条前开启描边并设置样式 - 添加
background(255):清空画布,避免每次draw()循环重复绘制导致画面混乱
内容的提问来源于stack exchange,提问作者Khalid
相关产品推荐
相关产品推荐

