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

         考虑一个矩形 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 个角的坐标。 

下面是实现过程:

// C# program to find corner points of 
// a rectangle using given length and middle 
// points. 
using System; 
 
class GFG 

 
    // Structure to represent a co-ordinate point 
    public class Point 
    { 
 
        public float x, y; 
 
        public Point() 
        { 
            x = y = 0; 
        } 
 
        public 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; 
        } 
 
        Console.Write((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 has been contributed by 29AjayKumar 

输出: 

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

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

时间复杂度: O(1) 

辅助空间: O(1)

  • 20
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Matlab对矩形HOG进行特征提取,需要先使用Matlab自带的HOG函数将图像转换为HOG特征向量,然后再从特征向量中提取矩形点。 以下是一种可能的实现方法: 1. 载入图像并进行预处理,将其转换为灰度图像(如果图像不是灰度图像的话)。 ``` img = imread('image.jpg'); gray_img = rgb2gray(img); ``` 2. 使用Matlab自带的`extractHOGFeatures`函数提取图像的HOG特征向量。 ``` cell_size = [8 8]; block_size = [2 2]; num_bins = 9; [hog_features, visualization] = extractHOGFeatures(gray_img, 'CellSize', cell_size, 'BlockSize', block_size, 'NumBins', num_bins); ``` 其中`cell_size`、`block_size`和`num_bins`是HOG特征提取的参数,可以根据需要进行调整。`hog_features`是提取出的HOG特征向量,`visualization`是可视化的HOG特征图像。 3. 将HOG特征向量重塑为原图像的大小,并使用Matlab自带的`hogtemplate`函数生成一个矩形HOG模板。 ``` hog_template = hogtemplate(size(gray_img), cell_size, block_size, num_bins); hog_features = reshape(hog_features, size(hog_template, 1), size(hog_template, 2), []); ``` 4. 对于每个矩形,从HOG特征向量中提取四个点。 ``` num_rects = size(rects, 1); % rects表示矩形的坐标和大小 corners = zeros(num_rects, 4, 2); % 存储矩形的四个点 for i = 1:num_rects rect_hog = hog_features(rects(i, 2):rects(i, 2)+rects(i, 4)-1, rects(i, 1):rects(i, 1)+rects(i, 3)-1, :); match_score = convn(rect_hog, hog_template, 'valid'); [max_val, max_ind] = max(match_score(:)); [max_y, max_x, max_c] = ind2sub(size(match_score), max_ind); corners(i, 1, :) = [max_x*cell_size(2), max_y*cell_size(1)]; corners(i, 2, :) = [(max_x+block_size(2))*cell_size(2), max_y*cell_size(1)]; corners(i, 3, :) = [(max_x+block_size(2))*cell_size(2), (max_y+block_size(1))*cell_size(1)]; corners(i, 4, :) = [max_x*cell_size(2), (max_y+block_size(1))*cell_size(1)]; end ``` 其中`rects`表示矩形的坐标和大小,`corners`是存储矩形的四个点的数组。对于每个矩形,从HOG特征向量中提取矩形区域,并在矩形区域内使用卷积运算和最大值搜索找到最匹配的HOG模板位置。根据模板位置和HOG特征向量的单元格大小计算矩形的四个点。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值