C#中二维平面上对角线的线交点 [英] Line intersections for diagonals on a 2d plane in C#

查看:33
本文介绍了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?

我将在需要高度优化的算法中使用它,因此正确的答案将是最快的计算方式.我不确定这是否可以在没有三角学的情况下完成.

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/sqrt(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

您必须处理所有可能的方向(x1x1>x2、...),但它们都与此对称.

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

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

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