python中的形状区域

时间:2019-09-09 19:26:54

标签: python turtle-graphics

因此,我有一个名为the_rectangle的函数,该函数运行两次以创建两个矩形。但是,我希望python找出每个矩形的Area并通过打印确定哪个更大(“更大的矩形Area是:) 然后打印(“较小的矩形区域是:)。我也希望它使用宽度和长度作为参数来找到每个区域的值。这可能吗?

import turtle
import math


def the_rectangle(width, length, color):
    turtle.color(color)
    turtle.begin_fill()
    turtle.forward(width)
    turtle.left(90)
    turtle.forward(length)
    turtle.left(90)
    turtle.forward(width)
    turtle.left(90)
    turtle.forward(length)
    turtle.left(90)
    turtle.end_fill()

def main():
    the_rectangle(200, 100, "red")
    turtle.penup()
    turtle.forward(300)
    turtle.pendown()
    the_rectangle(100, 250, "yellow")

main()

4 个答案:

答案 0 :(得分:0)

尝试将矩形设为类。这样,您就可以将所有数据集中在一个地方。

class Rectangle:
    def __init__(self, length, width, color): # Initialization
        self.length = length
        self.width = width
        self.color = color

    def draw(self, turtle): # Draws the rectangle
        turtle.color(color)
        turtle.begin_fill()
        turtle.forward(width)
        turtle.left(90)
        turtle.forward(length)
        turtle.left(90)
        turtle.forward(width)
        turtle.left(90)
        turtle.forward(length)
        turtle.left(90)
        turtle.end_fill()

    def area(self): # Calculates the area
         return self.length * self.width

要使用此类比较区域,只需使用以下代码即可。

def main():
    rectangle1 = Rectangle(200, 100, "red")
    a1 = rectangle1.area()
    rectangle2 = Rectangle(100, 250, "yellow")
    a2 = rectangle2.area()

    print("The bigger rectangle's area is:", str(max(a1, a2)))
    print("The smaller rectangle's area is:", str(min(a1, a2)))

    # Draw the rectangles
    rectangle1.draw()
    turtle.penup()
    turtle.forward(300)
    turtle.pendown()
    rectangle2.draw()

答案 1 :(得分:0)

如果您要确定哪个矩形更大,那么存储矩形(或其面积)将是至关重要的。您可以根据尺寸计算绘制矩形之前或之后的面积:


# This will allow you to create a rectangle and store its area
# as a three-element tuple
def area(l, w):
    # returns a three-element tuple to represent a rectangle
    return (l, w, l*w)

现在,您可以获取返回的矩形并进行绘制。我使用带有*的参数解包来解开维度,然后您知道您将重复绘制它的长度和宽度,从而减少了必须编写的额外代码量:

def draw_rectangle(*dimensions, color="red"):
    dims = dimensions[:2]*2 # creates a list [l, w, l, w]
    for dim in dims:
        turtle.forward(dim)
        turtle.left(90)

现在,要比较一个矩形,如果一个矩形的面积更大,则它的确比另一个大,因此,您可以选择任意数量的矩形,并使用itertools.combinations将它们配对。然后比较每对,然后可以将结果打印出来:

from itertools import combinations

def compare_recs(*rectangles):
    for r1, r2 in combinations(rectangles, 2):
        # The third element of your tuple, denoted by the index [2],
        # is your area, which is what you are comparing against here
        bigger, smaller = (r1, r2) if r1[2] >= r2[2] else (r2, r1)
        print(f"The bigger rectangle is {bigger}")
        print(f"The smaller rectangle is {smaller}")

r1 = area(10, 20)
r2 = area(10, 15)
draw_rectangle(*r1, color='red')
draw_rectangle(*r2, color='yellow')


compare_recs(r1, r2)
The bigger rectangle is (10, 20, 200)
The smaller rectangle is (10, 15, 150)

您可以使用类来做到这一点,但是我认为如果您是Python的新手,这是一个不错的起点。

答案 2 :(得分:0)

有很多方法可以完成所需的操作,但是对现有代码的最简单修改可能是让“ the_rectangle”返回该区域。 您需要获取返回值并计算最大/最小面积。

如果要使它成为一个规模更大的程序,则需要创建类,如其他答案中所述。

import turtle
import math


def the_rectangle(width, length, color):
    # This function draws a rectangle and returns the area
    turtle.color(color)
    turtle.begin_fill()
    turtle.forward(width)
    turtle.left(90)
    turtle.forward(length)
    turtle.left(90)
    turtle.forward(width)
    turtle.left(90)
    turtle.forward(length)
    turtle.left(90)
    turtle.end_fill()
    return width*length

def main():
    area_1 = the_rectangle(200, 100, "red")
    turtle.penup()
    turtle.forward(300)
    turtle.pendown()
    area_2 = the_rectangle(100, 250, "yellow")
    print("Largest rectangle is " + str(max(area_1,area_2)))
    print("Smallest rectangle is " + str(min(area_1,area_2)))

main()

答案 3 :(得分:0)

如果您不想更新现有功能the_rectangle,则可以创建一个装饰器功能,该功能将打印矩形区域,可以保存该区域,以后再与其他矩形区域进行比较以检查哪个区域较小而较大。

这是示例代码:

def area(function):
    def wrapper(*args):
        print('Area of rectangle %d*%d is: %d' % (args[0], args[1], args[0]*args[1]))
        return function(*args)
    return wrapper

@area
def the_rectangle(width, length, color):
    ...