本文介绍了六边形中不同颜色的线条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只是想知道如何在六边形的每一行上获得不同的颜色.下面我有所需的输出和输入.
立即输出 -
为了更接近目标图像,您需要将六边形(圆形)的各个部分绘制为梯形.
导入海龟从 itertools 导入循环颜色 = [#9c2921"、#f5d905"、#cf8e04"、]NUM_SIDES = 6SIDE_LENGTH = 50笔宽 = 30圆周半径 = SIDE_LENGTH海龟.width(1)turtle.speed('fastest') # 因为我没有耐心颜色 = 周期(颜色)对于范围内的 _(4):乌龟.penup()海龟.sety(-circumradius)乌龟.pendown()对于 _ 范围内(NUM_SIDES):龟.颜色(下一个(颜色))龟.circle(圆周半径,范围= 360/NUM_SIDES,步骤= 1)乌龟.right(90)海龟.begin_fill()乌龟.转发(PEN_WIDTH/2)乌龟.right(120)龟.forward(圆周半径 + PEN_WIDTH/2)乌龟.right(120)乌龟.转发(PEN_WIDTH/2)海龟.end_fill()海龟.begin_fill()乌龟.转发(PEN_WIDTH/2)乌龟.right(60)龟.forward(圆周半径 - PEN_WIDTH/2)乌龟.right(60)乌龟.转发(PEN_WIDTH/2)海龟.end_fill()龟.left(90)圆周半径 += PEN_WIDTH*2海龟.hideturtle()乌龟.完成()
I was just wondering on how to get different colors on each line of the hexagon. Below I have the desired output and input.
Output Right Now - Link to output right now
Output I want - Link to output I want
import turtle as trtl
colors = ["#9c2921", "#cf8e04","#f5d905",]
#--------------------
num_sides = int(input("Enter the number of sides(Enter 6 to get the output of the real image): "))
if num_sides == 6:
print("Desired artwork is displayed")
side_length = 25
circumradius = side_length
angle = 360/len(colors)
trtl.width(10)
for color in colors:
trtl.color(color)
trtl.pensize(10)
for move_turtle in range(1):
trtl.penup()
trtl.sety(-circumradius)
trtl.pendown()
trtl.circle(circumradius, steps = num_sides)
circumradius *= 2
trtl.hideturtle()
解决方案
Oddly, your program looks like code I wrote in response to your previous question that has neither been accepted nor upvoted. Moving on:
Given this circle()
and a fat pen based approach for drawing the hexagons, I believe this is about the best you can do:
import turtle
from itertools import cycle
COLORS = ["#9c2921", "#f5d905", "#cf8e04",]
NUM_SIDES = 6
SIDE_LENGTH = 50
PEN_WIDTH = 25
circumradius = SIDE_LENGTH
turtle.width(PEN_WIDTH)
color = cycle(COLORS)
for _ in range(4):
turtle.penup()
turtle.sety(-circumradius)
turtle.pendown()
for _ in range(NUM_SIDES):
turtle.color(next(color))
turtle.circle(circumradius, extent=360/NUM_SIDES, steps=1)
circumradius += PEN_WIDTH*2
turtle.hideturtle()
turtle.done()
To get closer to the target image, you'd need to draw the individual segments of the hexagon (circle) as trapezoids.
import turtle
from itertools import cycle
COLORS = ["#9c2921", "#f5d905", "#cf8e04",]
NUM_SIDES = 6
SIDE_LENGTH = 50
PEN_WIDTH = 30
circumradius = SIDE_LENGTH
turtle.width(1)
turtle.speed('fastest') # because I have no patience
color = cycle(COLORS)
for _ in range(4):
turtle.penup()
turtle.sety(-circumradius)
turtle.pendown()
for _ in range(NUM_SIDES):
turtle.color(next(color))
turtle.circle(circumradius, extent=360/NUM_SIDES, steps=1)
turtle.right(90)
turtle.begin_fill()
turtle.forward(PEN_WIDTH/2)
turtle.right(120)
turtle.forward(circumradius + PEN_WIDTH/2)
turtle.right(120)
turtle.forward(PEN_WIDTH/2)
turtle.end_fill()
turtle.begin_fill()
turtle.forward(PEN_WIDTH/2)
turtle.right(60)
turtle.forward(circumradius - PEN_WIDTH/2)
turtle.right(60)
turtle.forward(PEN_WIDTH/2)
turtle.end_fill()
turtle.left(90)
circumradius += PEN_WIDTH*2
turtle.hideturtle()
turtle.done()
这篇关于六边形中不同颜色的线条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!