我写了一个画房子的功能。
def drawHouse():
pic=makeEmptyPicture(400,400)
for x in range (0,400):
for y in range (0,400):
px =getPixel(pic,x,y)
if y <180:
#sky
color=makeColor(137, 194, 255)
if y>=180:
#grass
color =makeColor(137, 194, 19)
if(x>50 and x<200) and (y>160 and y<320):
#house walls
color=makeColor(215, 141, 21)
if (x>70 and x<110) and (y>200 and y<320):
#door
color=makeColor(17, 132, 176)
if ((x>120 and x <155) and (y>180 and y<240)) or ((x>160 and x<195) and (y>180 and y<240)):
#windows
color=makeColor(216, 255, 255)
x0=182
x1=43
x2=204
y0=79
y1=161
y2=162
if (x>((y-y0)*(x1-x0)/(y1-y0)+x0) and x<((y-y0)*(x2-x0)/(y2-y0)+x0) and x>((y-y2)*(x1-x2)/(y1-y2)+x2)):
#roof
color = makeColor(255, 0, 28)
setColor(px,color)
return pic
当我为屋顶创建一个三角形时。如何为屋顶创建一个带水平底边的三角形(不添加单独的直线)..给定x值需要可以被y点的差值整除(x>((y-y0)*(x1) -x0)/的(Y1-Y0) + X0)?
答案 0 :(得分:0)
当我输入问题时,我找到了答案。扭转变量并测试y。
def drawHouse():
pic=makeEmptyPicture(400,400)
for x in range (0,400):
for y in range (0,400):
px =getPixel(pic,x,y)
if y <180:
#sky
color=makeColor(137, 194, 255)
if y>=180:
#grass
color =makeColor(137, 194, 19)
if(x>50 and x<200) and (y>160 and y<320):
#house walls
color=makeColor(215, 141, 21)
if (x>70 and x<110) and (y>200 and y<320):
#door
color=makeColor(17, 132, 176)
if ((x>120 and x <155) and (y>180 and y<240)) or ((x>160 and x<195) and (y>180 and y<240)):
#windows
color=makeColor(216, 255, 255)
x0=182
x1=43
x2=204
y0=79
y1=161
y2=161
if (y>((x-x0)*(y1-y0)/(x1-x0)+y0) and y>((x-x0)*(y2-y0)/(x2-x0)+y0) and y<((x-x2)*(y1-y2)/(x1-x2)+y2)):
#roof
color = makeColor(255, 0, 28)
setColor(px,color)
return pic