线的交点在C#在2D平面对角线 [英] Line intersections for diagonals on a 2d plane in C#

查看:298
本文介绍了线的交点在C#在2D平面对角线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设一个二维网格左上方细胞作为(0,0)。选择任意两点/坐标,画出对角线和反对角线上的每个。它们可以相交内部或网格的外部

Assume a 2D grid with the top left cell as (0, 0). Pick any two points/coordinates and draw a diagonal and anti-diagonal on each. They may intersect inside or outside the grid.

在附图片,红线是对角线的两个点(300,200)和(700,800)。

In the picture attached, the red lines are diagonals to the two points (300, 200) and (700, 800).

我怎样才能找出对角线交点坐标?此外,如何将公式不同,如果直线的斜率为负?

How can I find out the coordinates for the diagonal intersections? Also, how would the formula differ if the slope of the line were negative?

我会在一个算法,需要进行高度优化,以便正确的答案是计算最快的方式使用。我不知道这是否可以不trignometry完成。

I will use this in an algorithm that needs to be highly optimized so the right answer would be the fastest possible way to compute. I'm not sure if this can be done without trignometry.

注意: 请记住,红色线是一个真正的对角/防斜对。换句话说,它们是在45度角的矩形。这可能会或可能不会帮助选择比矢量计算更优化的方案。

NOTE: Please keep in mind that the red lines are a true diagonal/anti-diagonal pair. In other words they are at 45 degree angles to the rectangle. This may or may not help select a more optimized formula than vector calculation.

推荐答案

D 是两个边长度之差。在你的身影, D = 200 。这是两个白色三角形(您的外部交点和矩形之间的那些)的斜边的长度。因此,这些三角形的边长是 D /开方(2)等外部交叉点的坐标,由 D从矩形角相差/ 2

Let D be the difference between the two side lengths. In your figure, D=200. This is the length of the hypotenuse of the two white triangles (the ones between your exterior intersection points and the rectangle). So the side lengths of those triangles are D/sqrt(2), and so the coordinates of the exterior intersections differ from the rectangle corners by D/2.

那么对于您的图表,

(x1,y1) = 300-D/2, 200+D/2 = 200,300
(x2,y2) = 700+D/2, 800-D/2 = 800,700

您将必须处理所有可能的方向( X1< X2 X1> X2 ,.. )。但它们都是对称的这一个。

You'll have to handle all the possible orientations (x1<x2, x1>x2, ...) but they are all symmetric to this one.

这篇关于线的交点在C#在2D平面对角线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆