0

我写了一个画房子的函数。

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)?

4

1 回答 1

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

房子图片

于 2013-08-05T12:48:03.667 回答