Java 使用中点查找矩形的角(Find Corners of Rectangle using mid points)

Java实现根据中点和边长求矩形顶点坐标

        考虑一个矩形 ABCD,我们给出了边 AD 和 BC 中点(分别为 p 和 q)的坐标以及它们的长度 L(AD = BC = L)。现在给定参数,我们需要打印 4 个点 A、B、C 和 D 的坐标。

例子: 

输入:p = (1, 0)
        q = (1, 2)
        L = 2

输出:(0,0),(0,2),(2,2),(2,0)

解释:

打印的点形成一个矩形,

满足输入约束。

输入:p = (1, 1)
        q = (-1, -1)
        L = 2*sqrt(2)

输出:(0,2),(-2,0),(0,-2),(2,0)

从问题陈述中可能出现 3 种情况:  

矩形是水平的,即 AD 和 BC 平行于 X 轴

矩形是垂直的,即 AD 和 BC 平行于 Y 轴

矩形与轴线呈一定角度倾斜

        前两种情况很简单,使用基本几何学就可以轻松解决。对于第三种情况,我们需要应用一些数学概念来找到点。

        为了清楚起见,请考虑上图。我们有 p 和 q 的坐标。因此,我们可以找到 AD 和 BC 的斜率(因为 pq 垂直于 AD)。一旦我们有了 AD 的斜率,我们就可以找到通过 AD 的直线方程。现在我们可以应用距离公式来获得沿 X 轴和 Y 轴的位移。 

如果 AD 的斜率 = m,则 m = (px- qx)/(qy- py)

以及沿 X 轴的位移,dx = L/(2*sqrt(1+m*m))

类似地,dy = m*L/(2*sqrt(1+m*m))

现在,我们可以通过简单地加减相应获得的位移来找到 4 个角的坐标。 

下面是实现过程:

// Java program to find corner points of 
// a rectangle using given length and middle 
// points. 
 
class GFG 
{
 
    // Structure to represent a co-ordinate point 
    static class Point 
    {
 
        float x, y;
 
        Point() 
        {
            x = y = 0;
        }
 
        Point(float a, float b) 
        {
            x = a;
            y = b;
        }
    };
 
    // This function receives two points and length 
    // of the side of rectangle and prints the 4 
    // corner points of the rectangle 
    static void printCorners(Point p, Point q, float l) 
    {
        Point a = new Point(), b = new Point(),
                c = new Point(), d = new Point();
 
        // horizontal rectangle 
        if (p.x == q.x) 
        {
            a.x = (float) (p.x - (l / 2.0));
            a.y = p.y;
 
            d.x = (float) (p.x + (l / 2.0));
            d.y = p.y;
 
            b.x = (float) (q.x - (l / 2.0));
            b.y = q.y;
 
            c.x = (float) (q.x + (l / 2.0));
            c.y = q.y;
        } 
        // vertical rectangle 
        else if (p.y == q.y)
        {
            a.y = (float) (p.y - (l / 2.0));
            a.x = p.x;
 
            d.y = (float) (p.y + (l / 2.0));
            d.x = p.x;
 
            b.y = (float) (q.y - (l / 2.0));
            b.x = q.x;
 
            c.y = (float) (q.y + (l / 2.0));
            c.x = q.x;
        } 
        // slanted rectangle 
        else
        {
            // calculate slope of the side 
            float m = (p.x - q.x) / (q.y - p.y);
 
            // calculate displacements along axes 
            float dx = (float) ((l / Math.sqrt(1 + (m * m))) * 0.5);
            float dy = m * dx;
 
            a.x = p.x - dx;
            a.y = p.y - dy;
 
            d.x = p.x + dx;
            d.y = p.y + dy;
 
            b.x = q.x - dx;
            b.y = q.y - dy;
 
            c.x = q.x + dx;
            c.y = q.y + dy;
        }
 
        System.out.print((int)a.x + ", " + (int)a.y + " \n"
                + (int)b.x + ", " + (int)b.y + "\n"
                + (int)c.x + ", " + (int)c.y + " \n"
                + (int)d.x + ", " + (int)d.y + "\n");
    }
 
    // Driver code 
    public static void main(String[] args)
    {
        Point p1 = new Point(1, 0), q1 = new Point(1, 2);
        printCorners(p1, q1, 2);
 
        Point p = new Point(1, 1), q = new Point(-1, -1);
        printCorners(p, q, (float) (2 * Math.sqrt(2)));
    }
}
 
// This code contributed by Rajput-Ji

输出: 

0,0
0、2
2,2
2,0

0、2
-2,0
0、-2
2,0

时间复杂度: O(1) 

辅助空间: O(1)

### 使用 `find_blobs()` 查找矩形框的四个 在图像处理中,`find_blobs()` 函数可以用于识别图像中的颜色块。通过设定合适的颜色阈值,可以在图像中检测出特定的颜色区域。若目标是识别一个矩形框的四个,可以将这四个设计为具有特定颜色的小色块,并通过 `find_blobs()` 分别识别这些[^1]。 具体实现中,首先需要为每个定义对应的颜色阈值。例如,可以使用不同颜色(如红色、蓝色、绿色、黄色)来表示四个,或者使用相同颜色但确保它们在图像中是唯一的色块。随后,调用 `find_blobs()` 函数分别检测这些颜色阈值对应的色块,从而获取四个的位置信息。 以下是一个示例代码,展示如何使用 `find_blobs()` 识别四个: ```python import sensor, image, time sensor.reset() sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QVGA) sensor.skip_frames(time=2000) # 定义四个的颜色阈值 red_threshold = (0, 100, 10, 127, -0, 40) # 红色 blue_threshold = (0, 30, -60, -10, -30, 0) # 蓝色 green_threshold = (20, 100, -50, 0, 0, 50) # 绿色 yellow_threshold = (30, 100, -10, 60, 10, 127) # 黄色 while True: img = sensor.snapshot() # 检测四个 red_blobs = img.find_blobs([red_threshold]) blue_blobs = img.find_blobs([blue_threshold]) green_blobs = img.find_blobs([green_threshold]) yellow_blobs = img.find_blobs([yellow_threshold]) # 提取每个的坐标 corners = [] if red_blobs: corners.append(red_blobs[0].cx(), red_blobs[0].cy()) # 红色 if blue_blobs: corners.append(blue_blobs[0].cx(), blue_blobs[0].cy()) # 蓝色 if green_blobs: corners.append(green_blobs[0].cx(), green_blobs[0].cy()) # 绿色 if yellow_blobs: corners.append(yellow_blobs[0].cx(), yellow_blobs[0].cy()) # 黄色 # 绘制四个 for corner in corners: img.draw_circle(corner[0], corner[1], 5, color=(255, 255, 255)) if len(corners) == 4: print("四个已识别:", corners) else: print("未检测到完整的四个。") ``` 通过上述方法,可以有效地识别矩形框的四个,并进一步用于几何分析或图像校正等任务。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hefeng_aspnet

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值