python draw.rectangle_Python 问题,class rectangle

CompleteclassRectangleCollection,whichhasonelistinstancevariable,rectangles,thatshouldinitiallyrefertoanemptylist.Writeamethodadd_rectanglethattakesaRectangleasaparameter...

Complete class RectangleCollection, which has one list instance variable, rectangles, that should initially refer to an empty list. Write a methodadd_rectangle that takes a Rectangle as a parameter and appends it to the rectangles list. Write a method get_same_area_rects that takes a number as a parameter and returns a list of all Rectangles from the rectangles list that have that area.

class Rectangle:

""" A rectangle with a width and height. """

def __init__(self, w, h):

""" (Rectangle, number, number)

Create a new rectangle of width w and height h.

>>> r = Rectangle(1, 2)

>>> r.width

1

>>> r.height

2

"""

self.width = w

self.height = h

def get_area(self):

""" (Rectangle) -> number

Return the area of this rectangle.

>>> r = Rectangle(10, 20)

>>> r.get_area()

200

"""

return self.width * self.height

class RectangleCollection:

def __init__(self):

""" (RectangleCollection) -> NoneType

>>> rc = RectangleCollection()

>>> rc.rectangles

[]

"""

展开

结合以下代码 加入帧开始帧结束 import sensor import image import time from pyb import UART import struct # 导入struct模块 typecode ='bi' code = '' sensor.reset() sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QQVGA) sensor.skip_frames(time=2000) clock = time.clock() # 初始化UART uart = UART(3, 115200) # 根据实际情况修改UART的端口和波特率 # 定义一个结构体类型和一个结构体变量 class OpenmvDataStruct: def __init__(self, shape, num): self.shape = shape self.num = num data = OpenmvDataStruct('N', 0) # 初始值为shape为'N',num为0 while True: clock.tick() img = sensor.snapshot().lens_corr(1.8) # 检测圆形 for c in img.find_circles(threshold=3500, x_margin=10, y_margin=10, r_margin=10, r_min=2, r_max=100, r_step=2): img.draw_circle(c.x(), c.y(), c.r(), color=(255, 0, 0)) print('圆形') data.shape = 'C' #标识为C data.num = 1 img = sensor.snapshot() # 检测矩形 for r in img.find_rects(threshold=10000): img.draw_rectangle(r.rect(), color=(255, 0, 0)) for p in r.corners(): img.draw_circle(p[0], p[1], 5, color=(0, 255, 0)) print('矩形') data.shape = 'R' #标识为2 data.num = 2 # 检测三角形 sum_theta = 0 count = 0 for l in img.find_line_segments(merge_distance=10, max_theta_diff=10): img.draw_line(l.line(), color=(255, 0, 0)) sum_theta += l.theta() count += 1 avg_theta = sum_theta / count if count > 0 else 0 if 1 < avg_theta < 75: print('三角形') data.shape = 'T' #标识为T data.num = 3 print("FPS %f" % clock.fps()) # 将结构体变量data打包成字节流,并发送给Arduino # 打包data为字节流 packed_data = struct.pack(typecode, ord(data.shape), data.num) print(ord(data.shape)) uart.write(packed_data) # 发送数据
07-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值