所以我试图弄清楚如何实现在绘图区域中选择线条或边缘的方法,但我的数学有点缺乏.这是我到目前为止所得到的:
我知道我可以遍历行列表,但我不知道如何构造一个算法来通过给定的坐标(即鼠标点击)选择一条线.有人有任何想法或指出我正确的方向?
// import java.awt.Point
public Line selectLine(Point mousePoint) {
for (Line l : getLines()) {
Point start = l.getStart();
Point end = l.getEnd();
if (canSelect(start, end, mousePoint)) {
return l; // found line!
}
}
return null; // could not find line at mousePoint
}
public boolean canSelect(Point start, Point end, Point selectAt) {
// How do I do this?
return false;
}
Run Code Online (Sandbox Code Playgroud) 如果我System.Drawing.Rectangle在画布和一个上有两个对象,计算哪个( 的任何部分,而不仅仅是它的)最接近那个Point的最佳方法是什么?RectangleRectangleLocation PointPoint
一个单元测试的例子:
Rectangle one = new Rectangle (0, 0, 10, 10);
Rectangle two = new Rectangle (20, 20, 10, 10);
Point point = new Point(14, 14);
Rectangle actual = ClosestToPoint(point, one, two);
// should be closer to one since one's bottom right is at (10, 10)
Assert.That(actual, Is.SameAs(one));
// method to write
public Rectangle ClosestToPoint(Point p, params Rectangle[] rectangles) { }
Run Code Online (Sandbox Code Playgroud) 我正在用 C# 编写一个类似绘画的程序。我希望能够在单击靠近一条线时擦除该线(例如,距离 < 10 像素)。我尝试了不同的计算,但最终遇到的问题是,只有当我单击线条的起点或终点附近时,线条才会被删除。介于两者之间的任何事情似乎都不起作用。
令 p 为用户在表单中单击的点,startp 和 endp 为线条的终点。
double a = (endp.Y - startp.Y) / (endp.X - startp.X); // gradient
double b = endp.Y - a * endp.X; // y intercept
// condition such that it only works when i click close to the line segment,
// not the entire infinite line for which the calculation y=ax+b works
double yvalue = p.X * a + b; // value the line segment has at the x-value which …Run Code Online (Sandbox Code Playgroud) 我需要从给定CLLocationCoordinate2D的数组中找到最近的点GMSPolyline.GMSPath如果那更好的话,我可以将其转换为.是否有任何现成的方法(或任何存储库)进行此类计算?我在实施方面遇到了一些问题.我想知道如何创建一个算法:
1. for all polylines
1.1. find smallest distance between polyline and touch point, save CLLocationCoordinate2D
2. for all distances from point 1.1.
2.1. find the shortest one, it's CLLocationCoordinate2D is our point
Run Code Online (Sandbox Code Playgroud)
现在的问题是如何实现1.1点?
基于SOF最短距离问题,我写了这样的代码:
- (void)findNearestLineSegmentToCoordinate:(CLLocationCoordinate2D)coordinate {
GMSPolyline *bestPolyline;
double bestDistance = DBL_MAX;
CGPoint originPoint = CGPointMake(coordinate.longitude, coordinate.latitude);
for (GMSPolyline *polyline in self.polylines) {
polyline.strokeColor = [UIColor redColor]; // TMP
if (polyline.path.count < 2) { // we need at least 2 …Run Code Online (Sandbox Code Playgroud) c# ×2
geometry ×2
2d ×1
algorithm ×1
google-maps ×1
graphics ×1
ios ×1
java ×1
objective-c ×1
opencv ×1
polyline ×1
python-3.x ×1