mercredi 15 juin 2016

Using a graphics module. Draw a circle inside a rectangle. Make the circle move.

So I was able to draw a circle inside a rectangle using this graphics module we made in class. However, I cannot find out how to get the circle to move and bounce of the rectangle walls. How do I accomplish this?

import turtle
from numpy import linspace
from scipy import pi, sin, cos

class Canvas(object):
    def __init__(self, w, h):
        self.width = w
        self.height = h
        self.Objects = []
        self.turtle = turtle.Turtle()
        self.screen = turtle.Screen()
        self.screen.setup(width=self.width, height=self.height)

        self.turtle.hideturtle()

    def addShape(self, shape):
        self.Objects.append(shape)

    def draw(self, gObject):
        self.addShape(gObject)
        gObject.addToCanvas(self)
        self.turtle.up()
        self.screen.tracer(0)
        gObject._draw(self.turtle)
        self.screen.tracer(1)

    def redraw(self):
        self.screen.clearscreen()
        self.turtle.reset()
        self.screen.tracer(0)
        for shape in self.Objects:
            self.turtle.up()
            shape._draw(self.turtle)
        self.screen.tracer(1)
        self.turtle.hideturtle()

    def freeze(self):
        self.screen.exitonclick()


class GeometricObject(object):
    def __init__(self, color, width):
        self.Color = color
        self.Width = width
        self.myCanvas = None

    def setColor(self, color):
        self.Color = color
        self.myCanvas.redraw()

    def setWidth(self, width):
        self.Width = width
        self.myCanvas.redraw()

    def getColor(self):
        return self.Color

    def getWidth(self):
        return self.Width

    def addToCanvas(self, aCanvas):
        self.myCanvas = aCanvas

    def _draw(self):
        raise NotImplementedError

class Point(GeometricObject):
    def __init__(self, x, y, color='black', width=1):
        super().__init__(color, width)
        self.X = x
        self.Y = y

    def getX(self):
        return self.X

    def getY(self):
        return self.Y

    def getCoord(self):
        return self.X, self.Y

    def _draw(self, aturtle):
        aturtle.down()
        aturtle.goto(self.getX(), self.getY())
        aturtle.dot(self.Width, self.Color)

class Line(GeometricObject):
    def __init__(self, p1, p2, color='black', width=1):
        super().__init__(color, width)
        self.P1 = p1
        self.P2 = p2

    def getP1(self):
        return self.P1

    def getP2(self):
        return self.P2

    def _draw(self, aturtle):
        aturtle.color(self.getColor())
        aturtle.width(self.getWidth())
        aturtle.goto(self.P1.getCoord())
        aturtle.down()
        aturtle.goto(self.P2.getCoord())

class Shape(GeometricObject):
    def __init__(self, linecolor='black', linewidth=1, fillcolor=None):
        super().__init__(linecolor, linewidth)
        self.fillColor = fillcolor

    def setFill(self, acolor):
        self.fillColor = acolor
        self.myCanvas.redraw()


def ellipse(centerPoint, xradius, yradius, ang=pi, Nb=100):
    if xradius >= yradius:
        ra = max(xradius * 2, yradius * 2)
        rb = min(xradius * 2, yradius * 2)
    else:
        ra = min(xradius * 2, yradius * 2)
        rb = max(xradius * 2, yradius * 2)
    x0 = centerPoint.X
    y0 = centerPoint.Y

    xpos, ypos = x0, y0
    radm, radn = ra, rb
    an = ang

    co, si = cos(an), sin(an)
    the = linspace(0, 2 * pi, Nb)
    X = radm * cos(the) * co - si * radn * sin(the) + xpos
    Y = radm * cos(the) * si + co * radn * sin(the) + ypos
    return X, Y

class Ellipse(Shape):
    def __init__(self, centerPoint, xradius, yradius, linecolor='black', linewidth='1',fillcolor=None):
        super().__init__(linecolor, linewidth, fillcolor)
        self.xRadius = xradius
        self.yRadius = yradius
        self.Center = centerPoint

    def getXRadius(self):
        return self.xRadius

    def getYRadius(self):
        return self.yRadius

    def getCenter(self):
        return self.Center

    def _draw(self, aturtle):
        aturtle.color(self.getColor())
        X, Y = ellipse(self.getCenter(), self.getXRadius(), self.getYRadius())

        if self.fillColor:
            aturtle.fillcolor(self.fillColor)
            aturtle.begin_fill()

        aturtle.goto(X[0], Y[0])
        aturtle.down()
        for i in range(1, len(X)):
            aturtle.goto(X[i], Y[i])

        if self.fillColor:
            aturtle.end_fill()

class Circle(Ellipse):
    def __init__(self, centerPoint, radius, linecolor='black', linewidth=1, fillcolor='green'):
        super().__init__(centerPoint, radius, radius, linecolor, linewidth, fillcolor)
        self.Radius = radius

    def getRadius(self):
        return self.Radius

    def circleMove(self):
        while i in range(1000):
            circle.move(dy,dx)
            sleep(0.05)

#POLYGONS
class Polygon(Shape):
    def __init__(self, plist, linecolor='black', linewidth='1', fillcolor=None):
        super().__init__(linecolor, linewidth, fillcolor)
        self.corners = plist

    def _draw(self, aturtle):
        aturtle.color(self.getColor())
        aturtle.width(self.getWidth())

        if self.fillColor:
            aturtle.fillcolor(self.fillColor)
            aturtle.begin_fill()

        aturtle.goto(self.corners[0].getCoord())
        aturtle.down()

        for idx in range(1, len(self.corners)):
            aturtle.goto(self.corners[idx].getCoord())

        aturtle.goto(self.corners[0].getCoord())
        if self.fillColor:
            aturtle.end_fill()

class Triangle(Polygon):
    def __init__(self, point1, point2, point3, linecolor='black', linewidth='1', fillcolor=None):
        super().__init__([point1, point2, point3], linecolor, linewidth, fillcolor)
        self.Point1 = point1
        self.Point2 = point2
        self.Point3 = point3

    def getPoint1(self):
        return self.Point1

    def getPoint2(self):
        return self.Point2

    def getPoint3(self):
        return self.Point3

class Rectangle(Polygon):
    def __init__(self, llcorner, urcorner, linecolor='black', linewidth='1', fillcolor=None):
        super().__init__([llcorner, Point(llcorner.getX(), urcorner.getY()), 
                          urcorner, Point(urcorner.getX(), llcorner.getY())], linecolor, linewidth, fillcolor)

        self.LLcorner = llcorner
        self.URcorner = urcorner

    def getLLcorner(self):
        return self.LLcorner

    def getURcorner(self):
        return self.URcorner


class Square(Rectangle):
    def __init__(self, llcorner, sideLength, linecolor='black', linewidth='1', fillcolor=None):
        super().__init__(llcorner, Point(llcorner.getX() + sideLength, llcorner.getY() + sideLength),
                         linecolor, linewidth, fillcolor)
        self.SideLength = sideLength

    def getSideLength(self):
        return self.SideLength


###############################################
#Create a Canvas
aCanvas = Canvas(1200, 700)

#Draw some Points
p1 = Point(1, 1, color='magenta', width=4)
p2 = Point(-15, 100, color='aqua', width=2)

#Draw a Line
line1 = Line(p1, p2)
line2 = Line(Point(100, 200), Point(200, -100), color='blue', width=3)
#aCanvas.draw(line1)

circle1 = Circle(Point(100, -15), 15, linecolor='black')

#Draw a Rectangle
rectangle1 = Rectangle(Point(12, -47), Point(220, 45), fillcolor='magenta')
aCanvas.draw(rectangle1)
aCanvas.draw(circle1)

aCanvas.freeze()

Aucun commentaire:

Enregistrer un commentaire