2017-08-04 7 views
0

Pourquoi: En train d'essayer/faire des trucs de Deeplearning, complètement nouveau pour python. Nous avons déjà du code en cours d'exécution pour nos affaires. Nous voulons maintenant compter les "choses" que nous trouvons. Parce que nous ne trouvons pas de données classifiées sur plusieurs chats et chiens. Je veux créer des images générées au hasard avec des hexagones.comment faire pivoter un polygone?

Comme je veux faire tourner certains d'entre eux. Mais je ne sais pas comment.

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' n'est pas un module standard de Python. Qu'est-ce que c'est et où l'avez-vous eu? Juste dire que vous "voulez" faire pivoter quelque chose, n'est pas assez d'informations, en plus de la quantité de rotation, vous devez également définir le point sur lequel la rotation doit se produire. – martineau

+2

Vous pourriez trouver [** _ Rotation de la ligne autour du point central donné deux sommets _ **] (https://stackoverflow.com/questions/14842090/rotate-line-around-center-point-given-two-vertices) utile. – martineau

Répondre

1

Voici comment appliquer la technique et les mathématiques dans my answer à une question similaire qui peut accomplir ce que vous « voulez » (si vous voulez les tourner autour de leurs points centraux).

Je l'ai testé avec la version 5.0 du module graphics que j'ai téléchargé à partir de:

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

Comme je l'ai mentionné dans un commentaire, ce qui crée des polygones ayant subi une rotation de cette façon -en créant d'abord un polygone non-réorienté, en le clonant, puis en faisant tourner la copie-est quelque peu inefficace, car il pourrait être accompli par cr manger des points tournés d'abord, puis créer le Polygon.

est ici une implémentation qui fait que:

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

Merci beaucoup, c'est exactement ce que je "veux" faire :). beaucoup d'amour, ont des jours très impressionnants <3 – Tollpatsch

+0

Tollpatsch: Bon à entendre. FWIW, il serait plus efficace de simplement générer le polygone pivoté depuis le début. Cela pourrait être fait en changeant 'drawahexagon()' donc il a appliqué la transformation qui est faite dans 'rotatePolygon()' aux valeurs x et y de toutes les coordonnées avant ou pendant la création de l'objet 'Polygon' qu'il retourne. – martineau

+0

très belle idée, mais comment puis-je obtenir le centre "point" sans dessiner l'hexagone en premier? Une idée de comment je tire un hexagone hors du centre? Je vais essayer de le découvrir moi-même, mais d'abord j'ai besoin de quelques heures de sommeil :) – Tollpatsch