2017-08-04 18 views
0

なぜ: 現在、いくつかのDeeplearningものを試しています。全く新しいPythonです。 私たちはすでにいくつかのコードを実行しています。私たちは今、私たちが見つけた「もの」を数えたいと思っています。複数の猫や犬についての分類されたデータは見つけられないからです。私は、六角形でランダムに生成された画像を作成したいと思います。ポリゴンを回転させる方法は?

どのように私はそれらの一部を回転させたいですか?しかし、私は方法を知らない。 (あなたが彼らの中心点についてのそれらを回転したい場合)ここで

from graphics import * 
from random import randint 
import math 

image_height = 1000 
image_height = 1000; 
def main(): 
    win = GraphWin("Window",image_height,image_height) 
    win.setBackground(color_rgb(255, 255, 255)) 

    for _ in range(0,8): 
      figure = drawahexagon(80) 
     #figure = rotatePolygon(figure,randint(0,90)) 
      figure.draw(win) 
    win.getMouse() 
    win.close 


def drawahexagon(length): 
    x = randint(0, image_height-length) 
    y = randint(0, image_height-length) 
    poly = Polygon(Point(x+getRandom(0),y+getRandom(0)), 
        Point(x+length+getRandom(1),y+getRandom(1)), 
        Point(x+(length*1.5)+getRandom(0),y+(length/2)+getRandom(1)), 
        Point(x+length+getRandom(1),y+length+getRandom(1)), 
        Point(x+getRandom(0),y+length+getRandom(1)), 
        Point(x-(length/2)+getRandom(1),y+(length/2)+getRandom(0))) 
    poly.setFill(color_rgb(255,0,0)) 
    return poly 


def getRandom(base): 
    if base == 0: 
    foo = randint(0,5) 
    else: 
    foo = randint(3,10) 
    return foo 



main() 
+2

'graphics'は、Pythonの標準モジュールではありません。

はここであることない実装です。それは何ですか?あなたはそれをどこで手に入れましたか?ちょうどあなたが何かを回転させたいと言っているだけでは十分な情報ではありません、回転の量の他に、スピンが起こるポイントを定義しなければなりません。 – martineau

+2

[** _2つの頂点がある場合、中心点を中心に線を回転する_ **](https://stackoverflow.com/questions/14842090/rotate-line-around-center-point-given-two-vertices)が役立ちます。 – martineau

答えて

1

は何を「したい」達成することができる同様の質問にはmy answerにおける技術や数学を適用する方法を説明します。

私はからダウンロードgraphicsモジュールのバージョン5.0でそれをテストした:私は回転ポリゴンをこのように作成し、コメントで述べたように

        http://mcsp.wartburg.edu/zelle/python/graphics.py


from graphics import * 
from random import randint 
from math import sin, cos, radians 

image_height = 1000 
image_height = 1000 

def main(): 
    win = GraphWin("Window", image_height, image_height) 
    win.setBackground(color_rgb(255, 255, 255)) 

    for _ in range(8): 
      figure = drawahexagon(80) 
      figure = rotatePolygon(figure, randint(0, 90)) 
      figure.draw(win) 
    try: 
     win.getMouse() # causes graphics.GraphicsError: getMouse in closed window 
    except GraphicsError: # ignore error 
     pass 
    win.close() 

def rotatePolygon(polygon, degrees): 
    """ Rotate polygon the given angle about its center. """ 
    theta = radians(degrees) # Convert angle to radians 
    cosang, sinang = cos(theta), sin(theta) 

    points = polygon.getPoints() 
    # find center point of Polygon to use as pivot 
    n = len(points) 
    cx = sum(p.getX() for p in points)/n 
    cy = sum(p.getY() for p in points)/n 

    new_points = [] 
    for p in points: 
     x, y = p.getX(), p.getY() 
     tx, ty = x-cx, y-cy 
     new_x = (tx*cosang + ty*sinang) + cx 
     new_y = (-tx*sinang + ty*cosang) + cy 
     new_points.append(Point(new_x, new_y)) 

    rotated_ploygon = polygon.clone() # clone to get current attributes 
    rotated_ploygon.points = new_points 
    return rotated_ploygon 

def drawahexagon(length): 
    x = randint(0, image_height-length) 
    y = randint(0, image_height-length) 
    poly = Polygon(Point(x+getRandom(0), y+getRandom(0)), 
        Point(x+length+getRandom(1), y+getRandom(1)), 
        Point(x+(length*1.5)+getRandom(0), y+(length/2)+getRandom(1)), 
        Point(x+length+getRandom(1), y+length+getRandom(1)), 
        Point(x+getRandom(0), y+length+getRandom(1)), 
        Point(x-(length/2)+getRandom(1), y+(length/2)+getRandom(0))) 
    poly.setFill(color_rgb(255, 0, 0)) 
    return poly 

def getRandom(base): 
    if base == 0: 
     foo = randint(0, 5) 
    else: 
     foo = randint(3, 10) 
    return foo 

main() 

最初に回転していないポリゴンを作成し、それを複製してからコピーを回転させることは、cr最初に回転した点を食べてからPolygonを作成します。

def drawarotatedhexagon(length, degrees): 
    x = randint(0, image_height-length) 
    y = randint(0, image_height-length) 
    points = [Point(x+getRandom(0), y+getRandom(0)), 
       Point(x+length+getRandom(1), y+getRandom(1)), 
       Point(x+(length*1.5)+getRandom(0), y+(length/2)+getRandom(1)), 
       Point(x+length+getRandom(1), y+length+getRandom(1)), 
       Point(x+getRandom(0), y+length+getRandom(1)), 
       Point(x-(length/2)+getRandom(1), y+(length/2)+getRandom(0))] 

    theta = radians(degrees) # Convert angle to radians 
    cosang, sinang = cos(theta), sin(theta) 

    n = len(points) 
    cx = sum(pt.getX() for pt in points)/n 
    cy = sum(pt.getY() for pt in points)/n 
    for pt in points: 
     tx, ty = pt.getX()-cx, pt.getY()-cy 
     nx = (tx*cosang + ty*sinang) + cx 
     ny = (-tx*sinang + ty*cosang) + cy 
     pt.x, pt.y = nx, ny 

    poly = Polygon(*points) 
    poly.setFill(color_rgb(255, 0, 0)) 
    return poly 
+0

非常に感謝しています、まさに私が "やりたい"ことです:)。多くの愛は、非常に素晴らしい日を過ごす< – Tollpatsch

+0

Tollpatsch:よく聞いてください。 FWIWでは、最初から回転したポリゴンを生成するほうが効率的です。これは 'drawahexagon()'を変更することによって行うことができます。これは、 'rotatePolygon()'で行われている変換を、返される 'Polygon'オブジェクトの作成前または作成中のすべての座標のxおよびy値に適用します。 – martineau

+0

非常に良いアイデアですが、どのように六角形を描くことなくセンター "ポイント"を得るのですか?どのように私は中心から六角形を描画する任意のアイデア?私は自分自身でそれを見つけようとしますが、まずは数時間寝る必要があります:) – Tollpatsch

関連する問題