Python - drawing a polygon

So, I'm trying to draw different polygons using a point class. I created a class called generate_polygon, which should generate a regular polygon with sides s, and x0, y0 is the starting vertex.

import sys
import math
import Line_Point 

try:
    x0 = float(sys.argv[1])
    y0 = float(sys.argv[2])
    s = int(sys.argv[3])
except:
    print >> sys.stderr, 'Syntax: generate_polygon.py x0 y0 s'
    sys.exit(1)

p1 = Line_Point.Point(x0, y0)
while s>0:
    p2 = Line_Point.Point(p1.x, p1.y)
    p2.rotate((2*math.pi)/s)
    line = Line_Point.Line(p1, p2)
    print >> sys.stdout, line
    p1=p2
    s = s-1

It also uses another class called Line_Point:

class Point:

    def __init__(self, x, y):
        if not isinstance(x, float):
            raise Error("Parameter \"x\" illegal.")
        self.x = x
        if not isinstance(y, float):
            raise Error ("Parameter \"y\" illegal.")
        self.y = y

def rotate(self, a):
        if not isinstance(a, float):
            raise Error("Parameter \"a\" illegal.")
        original_x = self.x
        original_y = self.y  # not necessary but looks cleaner
        self.x = math.cos(a) * original_x - math.sin(a) * original_y
        self.y = math.sin(a) * original_x + math.cos(a) * original_y

class Line:

    def __init__(self, point0, point1):
        self.point0 = Point(point0.x, point0.y)
        self.point1 = Point(point1.x, point1.y)

    def rotate(self, a):
        if not isinstance(a, float) or not isinstance(self.point0.x, float) or not isinstance(self.point1.y, float):
        raise Error("Parameter \"a\" illegal.")
    self.point0.rotate(a)
    self.point1.rotate(a)

I also use another class called rotate, which reads the lines from stdin and breaks them into 4 separate parts and rotates each part:

import sys
import Line_Point

try:
    a = float(sys.argv[1])
    count = int(sys.argv[2])
except:
    print >> sys.stderr, 'Syntax: rotate.py angle count'
    sys.exit(1)
for L in sys.stdin:
    L = L.split()
    print L
    x0 = float(L[0])
    y0 = float(L[1])
    x = float(L[2])
    y = float(L[3])
    p0 = Line_Point.Point(x0, y0)
    p1 = Line_Point.Point(x, y)
    line = Line_Point.Line(p0, p1)
for x in range(count):
    print L
    p0.rotate(a)

Thus, the expected output is a line file with four different parts of the line. If I test the code with python generate_polygon.py 0.0 250.0 3, then the expected result:

line 0 250 -217 -125
line -217 -125 217 -125
line 217 -125 0 250

But in the end I get:

0 250 -217 -125
-217 -125 217 125
217 125 217 125

, , , 4 . , generate_polygon rotate, , .

+4
1

, rotate Point . . self.x, . :

def rotate(self,a):
    original_x = self.x
    original_y = self.y  # not necessary but looks cleaner
    self.x = math.cos(a) * original_x - math.sin(a) * original_y
    self.y = math.sin(a) * original_x + math.cos(a) * original_y

while 2*math.pi/s - . . s , , ( ):

angle = 2 * math.pi / s
while s>0:
    p2 = Line_Point.Point(p1.x, p1.y)
    p2.rotate(angle)
    line = Line_Point.Line(p1, p2)
    print >> sys.stdout, line
    p1=p2
    s = s-1
0

All Articles