- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
这个问题是关于:
但是请注意,一个有趣的子问题在大多数解决方案中都被完全掩盖了,即使存在三个子情况,它们也只是为重合情况返回 null:
例如,我们可以像这样设计一个 C# 函数:
public static PointF[] Intersection(PointF a1, PointF a2, PointF b1, PointF b2)
其中 (a1,a2) 是一条线段,(b1,b2) 是另一条线段。
此函数需要涵盖大多数实现或解释所掩盖的所有奇怪情况。为了解决重合线的奇怪问题,该函数可以返回一个 PointF 数组:
最佳答案
// port of this JavaScript code with some changes:
// http://www.kevlindev.com/gui/math/intersection/Intersection.js
// found here:
// http://stackoverflow.com/questions/563198/how-do-you-detect-where-two-line-segments-intersect/563240#563240
public class Intersector
{
static double MyEpsilon = 0.00001;
private static float[] OverlapIntervals(float ub1, float ub2)
{
float l = Math.Min(ub1, ub2);
float r = Math.Max(ub1, ub2);
float A = Math.Max(0, l);
float B = Math.Min(1, r);
if (A > B) // no intersection
return new float[] { };
else if (A == B)
return new float[] { A };
else // if (A < B)
return new float[] { A, B };
}
// IMPORTANT: a1 and a2 cannot be the same, e.g. a1--a2 is a true segment, not a point
// b1/b2 may be the same (b1--b2 is a point)
private static PointF[] OneD_Intersection(PointF a1, PointF a2, PointF b1, PointF b2)
{
//float ua1 = 0.0f; // by definition
//float ua2 = 1.0f; // by definition
float ub1, ub2;
float denomx = a2.X - a1.X;
float denomy = a2.Y - a1.Y;
if (Math.Abs(denomx) > Math.Abs(denomy))
{
ub1 = (b1.X - a1.X) / denomx;
ub2 = (b2.X - a1.X) / denomx;
}
else
{
ub1 = (b1.Y - a1.Y) / denomy;
ub2 = (b2.Y - a1.Y) / denomy;
}
List<PointF> ret = new List<PointF>();
float[] interval = OverlapIntervals(ub1, ub2);
foreach (float f in interval)
{
float x = a2.X * f + a1.X * (1.0f - f);
float y = a2.Y * f + a1.Y * (1.0f - f);
PointF p = new PointF(x, y);
ret.Add(p);
}
return ret.ToArray();
}
private static bool PointOnLine(PointF p, PointF a1, PointF a2)
{
float dummyU = 0.0f;
double d = DistFromSeg(p, a1, a2, MyEpsilon, ref dummyU);
return d < MyEpsilon;
}
private static double DistFromSeg(PointF p, PointF q0, PointF q1, double radius, ref float u)
{
// formula here:
//http://mathworld.wolfram.com/Point-LineDistance2-Dimensional.html
// where x0,y0 = p
// x1,y1 = q0
// x2,y2 = q1
double dx21 = q1.X - q0.X;
double dy21 = q1.Y - q0.Y;
double dx10 = q0.X - p.X;
double dy10 = q0.Y - p.Y;
double segLength = Math.Sqrt(dx21 * dx21 + dy21 * dy21);
if (segLength < MyEpsilon)
throw new Exception("Expected line segment, not point.");
double num = Math.Abs(dx21 * dy10 - dx10 * dy21);
double d = num / segLength;
return d;
}
// this is the general case. Really really general
public static PointF[] Intersection(PointF a1, PointF a2, PointF b1, PointF b2)
{
if (a1.Equals(a2) && b1.Equals(b2))
{
// both "segments" are points, return either point
if (a1.Equals(b1))
return new PointF[] { a1 };
else // both "segments" are different points, return empty set
return new PointF[] { };
}
else if (b1.Equals(b2)) // b is a point, a is a segment
{
if (PointOnLine(b1, a1, a2))
return new PointF[] { b1 };
else
return new PointF[] { };
}
else if (a1.Equals(a2)) // a is a point, b is a segment
{
if (PointOnLine(a1, b1, b2))
return new PointF[] { a1 };
else
return new PointF[] { };
}
// at this point we know both a and b are actual segments
float ua_t = (b2.X - b1.X) * (a1.Y - b1.Y) - (b2.Y - b1.Y) * (a1.X - b1.X);
float ub_t = (a2.X - a1.X) * (a1.Y - b1.Y) - (a2.Y - a1.Y) * (a1.X - b1.X);
float u_b = (b2.Y - b1.Y) * (a2.X - a1.X) - (b2.X - b1.X) * (a2.Y - a1.Y);
// Infinite lines intersect somewhere
if (!(-MyEpsilon < u_b && u_b < MyEpsilon)) // e.g. u_b != 0.0
{
float ua = ua_t / u_b;
float ub = ub_t / u_b;
if (0.0f <= ua && ua <= 1.0f && 0.0f <= ub && ub <= 1.0f)
{
// Intersection
return new PointF[] {
new PointF(a1.X + ua * (a2.X - a1.X),
a1.Y + ua * (a2.Y - a1.Y)) };
}
else
{
// No Intersection
return new PointF[] { };
}
}
else // lines (not just segments) are parallel or the same line
{
// Coincident
// find the common overlapping section of the lines
// first find the distance (squared) from one point (a1) to each point
if ((-MyEpsilon < ua_t && ua_t < MyEpsilon)
|| (-MyEpsilon < ub_t && ub_t < MyEpsilon))
{
if (a1.Equals(a2)) // danger!
return OneD_Intersection(b1, b2, a1, a2);
else // safe
return OneD_Intersection(a1, a2, b1, b2);
}
else
{
// Parallel
return new PointF[] { };
}
}
}
}
测试代码如下:
public class IntersectTest
{
public static void PrintPoints(PointF[] pf)
{
if (pf == null || pf.Length < 1)
System.Console.WriteLine("Doesn't intersect");
else if (pf.Length == 1)
{
System.Console.WriteLine(pf[0]);
}
else if (pf.Length == 2)
{
System.Console.WriteLine(pf[0] + " -- " + pf[1]);
}
}
public static void TestIntersect(PointF a1, PointF a2, PointF b1, PointF b2)
{
System.Console.WriteLine("----------------------------------------------------------");
System.Console.WriteLine("Does " + a1 + " -- " + a2);
System.Console.WriteLine("intersect " + b1 + " -- " + b2 + " and if so, where?");
System.Console.WriteLine("");
PointF[] result = Intersect.Intersection(a1, a2, b1, b2);
PrintPoints(result);
}
public static void Main()
{
System.Console.WriteLine("----------------------------------------------------------");
System.Console.WriteLine("line segments intersect");
TestIntersect(new PointF(0, 0),
new PointF(100, 100),
new PointF(100, 0),
new PointF(0, 100));
TestIntersect(new PointF(5, 17),
new PointF(100, 100),
new PointF(100, 29),
new PointF(8, 100));
System.Console.WriteLine("----------------------------------------------------------");
System.Console.WriteLine("");
System.Console.WriteLine("----------------------------------------------------------");
System.Console.WriteLine("just touching points and lines cross");
TestIntersect(new PointF(0, 0),
new PointF(25, 25),
new PointF(25, 25),
new PointF(100, 75));
System.Console.WriteLine("----------------------------------------------------------");
System.Console.WriteLine("");
System.Console.WriteLine("----------------------------------------------------------");
System.Console.WriteLine("parallel");
TestIntersect(new PointF(0, 0),
new PointF(0, 100),
new PointF(100, 0),
new PointF(100, 100));
System.Console.WriteLine("----------------------------------------------------------");
System.Console.WriteLine("");
System.Console.WriteLine("----");
System.Console.WriteLine("lines cross but segments don't intersect");
TestIntersect(new PointF(50, 50),
new PointF(100, 100),
new PointF(0, 25),
new PointF(25, 0));
System.Console.WriteLine("----------------------------------------------------------");
System.Console.WriteLine("");
System.Console.WriteLine("----------------------------------------------------------");
System.Console.WriteLine("coincident but do not overlap!");
TestIntersect(new PointF(0, 0),
new PointF(25, 25),
new PointF(75, 75),
new PointF(100, 100));
System.Console.WriteLine("----------------------------------------------------------");
System.Console.WriteLine("");
System.Console.WriteLine("----------------------------------------------------------");
System.Console.WriteLine("touching points and coincident!");
TestIntersect(new PointF(0, 0),
new PointF(25, 25),
new PointF(25, 25),
new PointF(100, 100));
System.Console.WriteLine("----------------------------------------------------------");
System.Console.WriteLine("");
System.Console.WriteLine("----------------------------------------------------------");
System.Console.WriteLine("overlap/coincident");
TestIntersect(new PointF(0, 0),
new PointF(75, 75),
new PointF(25, 25),
new PointF(100, 100));
TestIntersect(new PointF(0, 0),
new PointF(100, 100),
new PointF(0, 0),
new PointF(100, 100));
System.Console.WriteLine("----------------------------------------------------------");
System.Console.WriteLine("");
while (!System.Console.KeyAvailable) { }
}
}
这是输出:
----------------------------------------------------------line segments intersect----------------------------------------------------------Does {X=0, Y=0} -- {X=100, Y=100}intersect {X=100, Y=0} -- {X=0, Y=100} and if so, where?{X=50, Y=50}----------------------------------------------------------Does {X=5, Y=17} -- {X=100, Y=100}intersect {X=100, Y=29} -- {X=8, Y=100} and if so, where?{X=56.85001, Y=62.30054}--------------------------------------------------------------------------------------------------------------------just touching points and lines cross----------------------------------------------------------Does {X=0, Y=0} -- {X=25, Y=25}intersect {X=25, Y=25} -- {X=100, Y=75} and if so, where?{X=25, Y=25}--------------------------------------------------------------------------------------------------------------------parallel----------------------------------------------------------Does {X=0, Y=0} -- {X=0, Y=100}intersect {X=100, Y=0} -- {X=100, Y=100} and if so, where?Doesn't intersect--------------------------------------------------------------lines cross but segments don't intersect----------------------------------------------------------Does {X=50, Y=50} -- {X=100, Y=100}intersect {X=0, Y=25} -- {X=25, Y=0} and if so, where?Doesn't intersect--------------------------------------------------------------------------------------------------------------------coincident but do not overlap!----------------------------------------------------------Does {X=0, Y=0} -- {X=25, Y=25}intersect {X=75, Y=75} -- {X=100, Y=100} and if so, where?Doesn't intersect--------------------------------------------------------------------------------------------------------------------touching points and coincident!----------------------------------------------------------Does {X=0, Y=0} -- {X=25, Y=25}intersect {X=25, Y=25} -- {X=100, Y=100} and if so, where?{X=25, Y=25}--------------------------------------------------------------------------------------------------------------------overlap/coincident----------------------------------------------------------Does {X=0, Y=0} -- {X=75, Y=75}intersect {X=25, Y=25} -- {X=100, Y=100} and if so, where?{X=25, Y=25} -- {X=75, Y=75}----------------------------------------------------------Does {X=0, Y=0} -- {X=100, Y=100}intersect {X=0, Y=0} -- {X=100, Y=100} and if so, where?{X=0, Y=0} -- {X=100, Y=100}----------------------------------------------------------
关于c# - 检测两条重合线段的重合子集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2255842/
我正在执行大型 ffdf 对象的子集,我注意到当我使用 subset.ff 时,它会生成大量 NA。我通过使用 ffwhich 尝试了另一种方法,结果要快得多,并且没有生成 NA。这是我的测试: li
我对 Prolog 有点陌生。我正在尝试编写一个函数subset(Set, Subset) 来确定Subset 是否是Set 的子集(duh)。另外,如果第二个参数没有实例化,它应该输出每个可能的子集
一、题目 给你一个整数数组 nums ,数组中的元素 互不相同 。返回该数组所有可能的子集(幂集)。 解集 不能 包含重复的子集。你可以按 任意顺序 返回解集。 二、示例 输入:nums = [1,2
我想从数据帧的操作中排除一列。当然,我可以在没有要排除的列的情况下复制数据框,但这似乎是一种解决方法。我认为必须有一种更简单的方法来进行子集化。 所以这个示例代码应该显示我在做什么。 df colMe
我有一个 SpatialPolygonsDataFrame我通过使用 readOGR 读取 shapefile 创建的在 rgdal包裹。我正在尝试使用它来使用 spsample 生成采样网格在 sp
我想解决一个简单的问题,但即使我尝试了很多不同的方法,我也找不到解决方案。我正在使用 SICStus Prolog (如果这很重要),并且我想获取列表的所有子列表/子集(我不知道哪个术语是正确的),其
我目前正在使用 shinyTable,它是 HandsonTable (https://github.com/trestletech/shinyTable) 的 shiny 兼容实现。巧合的是,我意识
我正在 Delphi 中构建一个表单,其中包含服务下拉列表和用于选择服务的附加组件网格。我获取的数据来自 API,并且我将服务的数据存储在 ADODataSet 中,如下所示: ID (integer
subset() 函数有问题。如何通过观察次数对我的数据框的一个因子进行子集化? NAME CLASS COLOR VALUE antonio
我想知道是否有任何简单的算法来比较一个散列是否是另一个散列的子集。 例如,如果 $HASH{A} = B; $HASH{B} = C; $HASH{C} = D; $HASH2{A} = B; $HA
这个问题在这里已经有了答案: Array combinations without repetition (1 个回答) 关闭 8 年前。 给定一个数组,如何在 postgresql 中找到一定大小
我有下一个程序。我应该如何在 main 中使用迭代器以显示总和为 0 的子集? 我的程序应该打印: 2 -2 5 -5 # include # include using namespace st
我正在寻找一个可以自定义的 Markdown 解析器,最好是 Javascript。特别是,我想删除使用实际 HTML 标签的选项。我尝试编辑摊牌的来源,但无法弄清楚。 Jquery 集成也很好,尽管
我有一个包含名称列表的文件(引用文件 1): Apple Bat Cat 我有另一个文件(引用文件 2),其中包含名称列表和详细信息引用: Apple bla blaa aaaaaaaaagggggg
我有两个带有排序行的文件。一个文件 (B) 是另一个文件 (A) 的子集。我想找到 A 中不在 B 中的所有行。理想情况下,我想创建一个包含这些行的文件 (C)。这在 Unix 中可能吗?我正在寻找一
我有一个包含肽序列的列的数据框,我只想保留字符串中没有内部“R”或“K”的行。 df1 <- data.frame( Peptide = c("ABCOIIJUHFSAUJHR", "AOFI
这个问题在这里已经有了答案: How to subset matrix to one column, maintain matrix data type, maintain row/column na
假设我有一个列表向量: library(tidyverse) d 2) # A tibble: 5 x 1 x 1 2 3 4 5 最佳答案 应该是 lengt
我自己从来没有运行过javadoc(无论是在命令行还是ant's javadoc task;我将使用ant)——我需要为我编写的库生成一个javadoc。 问题是我的 java 库被组织成几个包,在
假设一个多方加密方案,类似于答案:Encryption with multiple different keys? . 那是。一组键K可以用来破译密文。 有没有办法过期: K'⊆ K 这样 K \ K
我是一名优秀的程序员,十分优秀!