Python reflect image along each sides of the triangle -
i have created triangle positioned in centre of screen.
from pil import image, imagedraw gray = (190, 190, 190) im = image.new('rgba', (400, 400), white) points = (250, 250), (100, 250), (250, 100) draw = imagedraw.draw(im) draw.polygon(points, gray)
how duplicate image , reflect along each sides of triangle @ different random points. example...
plan: first find random point on edge of big triangle put smaller one, , rotate fits on edge.
suppose can access points of triangle
triangle.edges[0].x, triangle.edges[0].y, triangle.edges[1].x, etc
we can find arbitrary point first selecting edge, , "walk random distance next edge":
r = randint(3) # random integer between 0 , 2 first_edge = triangle.edges[r] second_edge = r == 2 ? triangle.edges[0] : triangle.edges[r + 1] ## next lines kind of pseudo-code r = randfloat(1) random_point = (second_edge - first_edge)*r + first_edge
our next problem how rotate triangle. if have done algebra might recognise this:
def rotatepointaroundorigin(point, angle): new_point = point() new_point.x = cos(angle)*point.x - sin(angle)*point.y new_point.y = sin(angle).point.x + cos(angle)*point.y return new_point
(see https://en.wikipedia.org/wiki/rotation_matrix)
in addition need determine how rotate triangle, , apply function above of points.
Comments
Post a Comment