随机几何分配(初学者Python)

时间:2015-05-06 16:57:49

标签: python

我遇到的问题是尝试使我的形状文件具有随机位置和大小,而不必将定义放在for循环中。这是我的课程。

# Module imports
import math
import turtle as t

# Class Definitions
class Circle:

    def __init__(self,x=0,y=0,r=0):
        self.center = Point(x,y)
        self.radius = r

    def circumference(self):
        result = (self.radius * 2) * math.pi
        return result

    def area(self):
        result = (self.radius ** 2) * math.pi
        return result

    def draw(self,fill_color,pen_color,pen_width):
        t.color(pen_color)
        t.fillcolor(fill_color)
        t.pensize(pen_width)
        t.penup()
        t.goto(self.center.x,self.center.y)
        t.pendown()
        t.begin_fill()
        t.circle(self.radius)
        t.end_fill()
        t.penup()


class Point:
    """Represents a point with an x and y coordinate"""

    def __init__(self, x=0, y=0):
        self.x = x
        self.y = y

    def show(self):
        print "(%d, %d)" % (self.x, self.y)

    def move_to(self,x,y):
        self.x = x
        self.y = y

    def distance(self,p):
        dx = self.x - p.x
        dy = self.y - p.y
        dist = math.sqrt(dx**2 + dy**2)
        return dist


class Rectangle:
    "represented with upper left corner as a Point, width, height"""

    def __init__(self,x=0,y=0,width=100,height=100):
        self.corner = Point(x,y)
        self.width = width
        self.height = height

    def go_to(self,x,y):
        self.corner.move_to(x,y)

    def area(self):
        result = self.width * self.height
        return result

    def perimeter(self):
        result = 2*self.width + 2*self.height
        return result

    def draw(self,fill_color,pen_color,pen_width):
        t.color(pen_color)
        t.fillcolor(fill_color)
        t.pensize(pen_width)
        t.penup()
        t.goto(self.corner.x,self.corner.y)
        t.pendown()
        t.begin_fill()
        t.goto(self.corner.x + self.width,self.corner.y)
        t.goto(self.corner.x + self.width,self.corner.y - self.height)
        t.goto(self.corner.x,self.corner.y - self.height)
        t.goto(self.corner.x,self.corner.y)
        t.end_fill()
        t.penup()


class Segment:

    def __init__(self,x1=0,y1=0,x2=100,y2=0):
        self.start = Point(x1,y1)
        self.end = Point(x2,y2)

    def length(self):
        return self.start.distance(self.end)

    def draw(self,pen_color,pen_width):
        t.color(pen_color)
        t.pensize(pen_width)
        t.penup()
        t.goto(self.start.x,self.start.y)
        t.pendown()
        t.goto(self.end.x,self.end.y)
        t.penup()


class Triangle:

    def __init__(self,x1=0,y1=0,x2=0,y2=0,x3=0,y3=0):
        self.start = Point(x1,y1)
        self.second = Point(x2,y2)
        self.third = Point(x3,y3)
        self.segmentf = Segment(x1,y1,x2,y2)
        self.segments = Segment(x2,y2,x3,y3)
        self.segmentt = Segment(x3,y3,x1,y1)

    def area(self):
        a = self.segmentf.length()
        b = self.segments.length()
        c = self.segmentt.length()
        s = (a + b + c) / 2
        result = (s*(s-a)*(s-b)*(s-c)) ** 0.5
        return result

    def perimeter(self):
        a = self.segmentf.length()
        b = self.segments.length()
        c = self.segmentt.length()
        result = a + b + c
        return result

    def draw(self,fill_color,pen_color,pen_width):
        t.color(pen_color)
        t.fillcolor(fill_color)
        t.pensize(pen_width)
        t.penup()
        t.goto(self.start.x,self.start.y)
        t.pendown()
        t.begin_fill()
        t.goto(self.second.x,self.second.y)
        t.goto(self.third.x,self.third.y)
        t.goto(self.start.x,self.start.y)
        t.end_fill()
        t.penup()

这是我的主要功能:

# Module imports
import math
import turtle
import random 
from Suter_Andrew_shapesfinal import *

# Globals
MAX_S = 50

# List Definitions
r = Rectangle(random.randint(-100,100),random.randint(-100,100),random.randint(0,100),random.randint(0,100))
t = Triangle(random.randint(-100,100),random.randint(-100,100),random.randint(-100,100),random.randint(-100,100),random.randint(-100,100),random.randint(-100,100))
c = Circle(random.randint(-100,100),random.randint(-100,100),random.randint(0,100))

shapes = [r,t,c]
colors = ["blue","green","red","purple","pink","yellow","orange","black","white"]

# Main body
for i in range(MAX_S):
    s_choice = random.choice(shapes)
    c_choice = random.choice(colors)
    if s_choice == r:
        r.draw(c_choice,c_choice,random.randint(0,10))
    elif s_choice == t:
        t.draw(c_choice,c_choice,random.randint(0,10))
    elif s_choice == c:
        c.draw(c_choice,c_choice,random.randint(0,10))

turtle.mainloop()

该功能正常,但问题是顶部的定义。谢谢你的阅读!

1 个答案:

答案 0 :(得分:0)

如果你想通过循环每次随机定义一个形状,你将不得不将它的实例化放在for循环中。没有办法解决这个问题。如果你在循环之外创建实例化,那么只有一个,你最终会在所有情况下重用相同的实例(如你所见)。

# Module imports
import math
import turtle
import random 
from Suter_Andrew_shapesfinal import *

randint = random.randint

# Globals
MAX_S = 50

# List Definitions
shapes = [Rectangle, Triangle, Circle]
colors = ["blue", "green", "red", "purple", "pink", "yellow", "orange", "black", "white"]

# Main body
for i in range(MAX_S):
    s_choice = random.choice(shapes)
    c_choice = random.choice(colors)
    if s_choice == Rectangle:
        r = Rectangle(*([randint(-100, 100) for x in range(2)] + [randint(0, 100) for x in range(2)]))
        r.draw(c_choice, c_choice, random.randint(0, 10))
    elif s_choice == Triangle:
        t = Triangle(*[randint(-100, 100) for x in range(6)])
        t.draw(c_choice, c_choice, random.randint(0, 10))
    elif s_choice == Circle:
        c = Circle(*([randint(-100, 100) for x in range(2)] + [randint(0, 100)]))
        c.draw(c_choice, c_choice, random.randint(0, 10))

turtle.mainloop()

我还使用list comprehension简化了这一点的参数位来生成random.randints列表,然后在将它传递给函数时使用列表前面的*将列表解包为每个参数自动。