2016-05-31 3 views
2

私は、(1)描画する形状の辺の数、(2)描画する大きさ、(3)描画する数、およびつかいます。カメはその形の多くを円の周りに配置します。すべてが円の中心に頂点を共有します。Pythonのカメの形状を制御しますか?

最後に形が重なり合うと、最後の形がすべての上に表示されますが、残りの形のように前の形の後ろに入れてください。

私は、誤って重なる図形の数が図形の辺の数に依存していると判断することができました。たとえば、三角形の場合は、1〜6の図形と重複しません。 7-12では、1つの図形が誤って重なってしまいます。 13-18では、2つの図形が正しく重なり合わない。等々。

これまでは、図形の最初と最後のグループをpoly1とpoly2と考えることを書いてきましたが、少なくともポリ2を描くように伝えるのが好きですポリ1。

主なもの:これはカメでも可能ですか?そしてもしそうなら、どうしたらいいですか? (3.5を使用)

編集:これはおそらく不可能だと思っています。私が聞いていることはすべて、カメは既存の形の上にしか描けないということです。しかし、明快さを追加するためにスクリーンショットを含めることも提案されました。ここにはカメが描くもののイメージがあります(3つの異なる色を持つ9つの三角形を描くように指示されたとき)。

enter image description here

私の目標は、それが最初に描かれたように、完全な三角形は、12 Oクロックで1アンダー、まだその前の上に隠れていることにすることです。

+0

最後の一番上の形。どうにかして、最後の形状に重なるように最初の形状の一部を再描画する必要があります。 (また、あなたの投稿が誰にでも完全にはっきりしないかもしれない、いくつかのスクリーンショットを投稿した場合に役立つかもしれない) –

答えて

1

これはおそらく不可能だと思っています。

あなたは亀の草刈りを過小評価します。ここでは、修正したい非対称の問題を呈する私の最初の例だ:

import math 
from itertools import cycle 
from turtle import Turtle, Screen 

COLORS = cycle(['red', 'green', 'blue', 'yellow']) 

def rotate_polygon(polygon, angle): 

    theta = math.radians(angle) 
    sin, cos = math.sin(theta), math.cos(theta) 

    return [(x * cos - y * sin, x * sin + y * cos) for x, y in polygon] 

def fill_polygon(turtle, polygon, color): 

    turtle.color(color) 

    for vertex in polygon: 
     turtle.goto(vertex) 

     if not turtle.filling(): 
      turtle.begin_fill() 

    turtle.end_fill() 

# triangle cursor 5x in size and X translated 50 pixels 
polygon = ((100, -28.85), (50, 57.75), (0, -28.85)) 

screen = Screen() 

yertle = Turtle(visible=False) 
yertle.penup() 

for angle in range(0, 360, 30): 
    rotated_polygon = rotate_polygon(polygon, angle) 
    color = next(COLORS) 
    fill_polygon(yertle, rotated_polygon, color) 

screen.exitonclick() 

OUTPUT

enter image description here

私たちは本当に初期の赤1の下できれいに隠れてする最後の黄色の三角形をしたいです、エッシャーの階段のように。私はそれが複数の重なりを持つので、このイラストを選んだ、最終的な黄色の三角形は、理論的には、赤を下回るだけでなく、赤に続く緑と青の両方を覆うべきである。同様に、最終的な黄色に先行する青色と緑色は、赤色を覆います。上記など

enter image description here

私のコードは、この特定のイラストを描くのに必要とされるよりも、より複雑であるが、以下の機能拡張をサポートするために追加の構造を必要とします:

一つのアプローチは、交差点をうまくすることはないだろう最新の三角形のその部分を描画します。もう1つの方法は、新しい三角形を描画することですが、アンダーラップする三角形の交点を再描画します。この後者のアプローチサザーランド-Hodgmanポリゴンクリッピングアルゴリズムを経由して交差点を取得するために、既存のPythonの関数を使用して、私は以下の実装です:turtle`はそれぞれを置く `ためだ

import math 
from itertools import cycle 
from turtle import Turtle, Screen 

COLORS = cycle(['red', 'green', 'blue', 'yellow']) 

def clip(subjectPolygon, clipPolygon): 

    # obtain this code from: 
    # https://rosettacode.org/wiki/Sutherland-Hodgman_polygon_clipping#Python 

    return outputList 

def rotate_polygon(polygon, angle): 

    theta = math.radians(angle) 
    sin, cos = math.sin(theta), math.cos(theta) 

    return [(x * cos - y * sin, x * sin + y * cos) for x, y in polygon] 

def fill_polygon(turtle, polygon, color): 

    turtle.color(color) 

    for vertex in polygon: 
     turtle.goto(vertex) 

     if not turtle.filling(): 
      turtle.begin_fill() 

    turtle.end_fill() 

# triangle cursor 5x in size and X translated 50 pixels 
polygon = ((100, -28.85), (50, 57.75), (0, -28.85)) 

screen = Screen() 

yertle = Turtle(visible=False) 
yertle.speed('slowest') # slowly so we can see redrawing 
yertle.penup() 

polygons = [] 
POLYGON, COLOR = 0, 1 

for angle in range(0, 360, 30): 
    rotated_polygon = rotate_polygon(polygon, angle) 
    color = next(COLORS) 
    fill_polygon(yertle, rotated_polygon, color) 
    polygons.append((rotated_polygon, color)) 

    # The -3 here is empirical and really should be calculated, an exercise for the reader 
    for forward, backward in enumerate(range(-3, 1 - len(polygons), -1)): 
     if polygons[forward] != polygons[backward]: 
      try: 
       intersection_polygon = clip(rotated_polygon, polygons[forward][POLYGON]) 
      except (IndexError, ZeroDivisionError): 
       break # because clip() can throw an error when no intersection 

      if intersection_polygon: 
       fill_polygon(yertle, intersection_polygon, polygons[forward][COLOR]) 
      else: 
       break # if no intersection, don't look any further 
     else: 
      break # avoid testing against polygons clockwise from this one (needs work) 

screen.exitonclick() 

OUTPUT

enter image description here

関連する問題