Algorithms—223.Rectangle Area

本文提供了一个简洁的方法来计算两个矩形相交部分的面积,并以此来得出两矩形面积之差。通过巧妙地利用数学公式,该方法能够处理各种边界条件,确保计算的准确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

思路:理清关系,考虑好各种情况。

public class Solution {
    public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
        return (H-F)*(G-E)+(D-B)*(C-A)-(((H>D?D:H)>(F>B?F:B))?((H>D?D:H)-(F>B?F:B)):0)*((G>C?C:G)>(E>A?E:A)?((G>C?C:G)-(E>A?E:A)):0);
    }
}


耗时:412ms,中下游


import sensor import lcd lcd.init() sensor.reset(dual_buff=True) sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QVGA) sensor.skip_frames(time = 1000) while True: img = sensor.snapshot() lcd.display(img)把这个代码融合到# Automatic RGB565 Color Tracking Example # # This example shows off single color automatic RGB565 color tracking using the CanMV Cam. import sensor, image, time print("Letting auto algorithms run. Don't put anything in front of the camera!") sensor.reset() sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QVGA) sensor.skip_frames(time = 2000) sensor.set_auto_gain(False) # must be turned off for color tracking sensor.set_auto_whitebal(False) # must be turned off for color tracking clock = time.clock() # Capture the color thresholds for whatever was in the center of the image. r = [(320//2)-(50//2), (240//2)-(50//2), 50, 50] # 50x50 center of QVGA. print("Auto algorithms done. Hold the object you want to track in front of the camera in the box.") print("MAKE SURE THE COLOR OF THE OBJECT YOU WANT TO TRACK IS FULLY ENCLOSED BY THE BOX!") for i in range(60): img = sensor.snapshot() img.draw_rectangle(r) print("Learning thresholds...") threshold = [50, 50, 0, 0, 0, 0] # Middle L, A, B values. for i in range(60): img = sensor.snapshot() hist = img.get_histogram(roi=r) lo = hist.get_percentile(0.01) # Get the CDF of the histogram at the 1% range (ADJUST AS NECESSARY)! hi = hist.get_percentile(0.99) # Get the CDF of the histogram at the 99% range (ADJUST AS NECESSARY)! # Average in percentile values. threshold[0] = (threshold[0] + lo.l_value()) // 2 threshold[1] = (threshold[1] + hi.l_value()) // 2 threshold[2] = (threshold[2] + lo.a_value()) // 2 threshold[3] = (threshold[3] + hi.a_value()) // 2 threshold[4] = (threshold[4] + lo.b_value()) // 2 threshold[5] = (threshold[5] + hi.b_value()) // 2 for blob in img.find_blobs([threshold], pixels_threshold=100, area_threshold=100, merge=True, margin=10): img.draw_rectangle(blob.rect()) img.draw_cross(blob.cx(), blob.cy()) img.draw_rectangle(r) print("Thresholds learned...") print("Tracking colors...") while(True): clock.tick() img = sensor.snapshot() for blob in img.find_blobs([threshold], pixels_threshold=100, area_threshold=100, merge=True, margin=10): img.draw_rectangle(blob.rect()) img.draw_cross(blob.cx(), blob.cy()) print(clock.fps())这是canmv软件中的让屏幕和亮起来并功能正常,产品是野火的k210
07-31
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值