1

Why: Currently trying/doing some Deeplearning stuff, completely new to python. We already got some code running for our stuff. We just now wanna count the "things" we find. Because we dont find any classified data about multiple cat and dogs. I wanna create some random generated images with hexagons.

How ever i wanna have some of them rotated. But i dont know how.

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()
martineau
  • 119,623
  • 25
  • 170
  • 301
Tollpatsch
  • 304
  • 4
  • 13
  • 3
    `graphics` is not a standard Python module. What is it and where did you get it? Just saying you "wanna" rotate something, isn't enough information, besides the amount of rotation, you must also define the point about which the spinning is to occur. – martineau Aug 04 '17 at 13:51
  • 2
    You might find [**_Rotate line around center point given two vertices_**](https://stackoverflow.com/questions/14842090/rotate-line-around-center-point-given-two-vertices) helpful. – martineau Aug 04 '17 at 13:54

1 Answers1

3

Here's how to apply the technique and math in my answer to a similar question that may accomplish what you "wanna" (if you want to rotate them about their center points).

I tested it with version 5.0 of the graphics module which I downloaded from:

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

As I mentioned in a comment, creating rotated polygons this way—by first creating an unrotated polygon, cloning that, and then rotating the copy—is somewhat inefficient, since it could be accomplished by creating rotated points first and then creating the Polygon.

Here's an implementation that does that:

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
martineau
  • 119,623
  • 25
  • 170
  • 301
  • Thanks like very much, thats exactly what i "wanna" do :). much love, have very awesome days <3 – Tollpatsch Aug 04 '17 at 20:00
  • Tollpatsch: Good to hear. FWIW, it would be more efficient to just generate the rotated polygon from the start. That could be done by changing `drawahexagon()` so it applied the transformation that is being done in `rotatePolygon()` to the all coordinates' x and y values before or during the creation of the `Polygon` object it returns. – martineau Aug 04 '17 at 20:31
  • very nice idea, but how do i get the center "point" without drawing the hexagon first? Any idea how i draw a hexagon out of the center? I will try do find it out on my own, but first i need some hours of sleep :) – Tollpatsch Aug 04 '17 at 21:18
  • You can store the points temporarily in a local list using basically the code you have now for initially selecting their coordinates. After just that, the "center" point can be calculate since it won't change given that it's the spot all other points will spin around. Then go through the list again and do the math operations on them. Once that's done you can then construct the `Polygon` using the updated values. Have a restful night `;-)`. – martineau Aug 04 '17 at 21:56
  • To construct a hexagon given only a center point (cx, cy), and a length, you could randomly pick six angles each larger than the last (i.e. pick six and sort them). You could then compute an (x, y) point from each one of angle by using `pt[i].x = (length * cos(ang[i])) + cx`, `pt[i].y = (length * sin(ang[i])) + cx`. To make it more irregular, you could vary the `length` value by a small amount on each one. – martineau Aug 04 '17 at 22:08
  • Just thought of another way to generate the hexagons. Start out creating a perfectly regular one, constructed with a center point at a convenient position like (0, 0) to be used as a ""prototype" for the rest. Afterwards generate the number of actual ones needed by repeatedly taking cloning the prototype and then perturbing each of its six points a random amount in the x _and_ y directions from where it was originally (i.e. distort it a bit). – martineau Aug 04 '17 at 22:51
  • Your formula is wrong and will have rotation happening in the wrong direction. The proper formula is `(x*c - y*s) + cx` and `(x*s + y*c) + cy` ... where `c` and `s` are `cosang` and `sinang`, respectively. – OneMadGypsy Oct 16 '21 at 17:38
  • @Michael Guidry: You're mistaken. The formula in my answer isn't a [trivial 2D rotation](https://en.wikipedia.org/wiki/Rotation_matrix#In_two_dimensions) around the origin, it's the concatenation of three 2D transformations: Tʀᴀɴsʟᴀᴛᴇ ─► Rᴏᴛᴀᴛᴇ ─► Uɴᴛʀᴀɴsʟᴀᴛᴇ as described in the [other answer](https://stackoverflow.com/a/14842362/355230) of mine linked to at the top of this answer. – martineau Oct 16 '21 at 18:15