如何找到直线上的点-目标c? [英] How can I find the points in a line - Objective c?

查看:123
本文介绍了如何找到直线上的点-目标c?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑一条从点A(x,y)到点B(p,q)的线.

Consider a line from point A (x,y) to B (p,q).

方法CGContextMoveToPoint(context, x, y);移动到点x,y,方法CGContextAddLineToPoint(context, p, q);将从点A绘制到点B.

The method CGContextMoveToPoint(context, x, y); moves to the point x,y and the method CGContextAddLineToPoint(context, p, q); will draw the line from point A to B.

我的问题是,我能找到线段覆盖的所有点吗?

My question is, can I find the all points that the line cover?

实际上,我需要知道确切的点,即终点B之前的x点.

Actually I need to know the exact point which is x points before the end point B.

请参阅此图片.

上面的行仅供参考.这条线可以有任何角度.我需要在B点之前的直线中的第5点.

The line above is just for reference. This line may have in any angle. I needed the 5th point which is in the line before the point B.

谢谢

推荐答案

您不应以像素为单位进行思考.坐标是浮点值. (x,y)处的几何点根本不需要是像素.实际上,您应该将像素视为坐标系统中的矩形.

You should not think in terms of pixels. Coordinates are floating point values. The geometric point at (x,y) does not need to be a pixel at all. In fact you should think of pixels as being rectangles in your coordinate system.

这意味着终点之前的x像素"实际上没有任何意义.如果像素是矩形,则水平移动与垂直移动的"x像素"数量不同.而且,如果您朝任何其他方向移动,则更难决定其含义.

This means that "x pixels before the end point" does not really makes sense. If a pixel is a rectangle, "x pixels" is a different quantity if you move horizontally than it is if you move vertically. And if you move in any other direction it's even harder to decide what it means.

根据您要尝试执行的操作,将像素概念转换为像素可能会或可能不容易.相反,最好做相反的事情,停止以像素为单位进行思考,并将您当前以像素为单位的所有表达转换为非像素.

Depending on what you are trying to do it may or may not be easy to translate your concepts in pixel terms. It's probably better, however, to do the opposite and stop thinking in terms of pixels and translate all you are currently expressing in pixel terms into non pixel terms.

还请记住,确切的像素取决于系统,并且您通常可以(也可以不)向系统查询有关该像素的信息(尤其是考虑到诸如视网膜显示器和所有与分辨率无关的功能之类的东西).

Also remember that exactly what a pixel is is system dependent and you may or may not, in general, be able to query the system about it (especially if you take into consideration things like retina displays and all resolution independent functionality).

我看到您编辑了问题,但是点"并不比像素"更精确.

I see you edited your question, but "points" is not more precise than "pixels".

但是,我会尽力为您提供可行的解决方案.至少当您以正确的术语重新阐述问题后,它才是可行的.

However I'll try to give you a workable solution. At least it will be workable once you reformulate your problem in the right terms.

您的问题,正确表达的应该是:

Your question, correctly formulated, should be:

给出笛卡尔空间中两个点AB并且距离delta,点C的坐标是什么,使得C在通过B,段BC的长度为delta?

Given two points A and B in a cartesian space and a distance delta, what are the coordinates of a point C such that C is on the line passing through A and B and the length of the segment BC is delta?

以下是该问题的解决方案:

Here's a solution to that question:

// Assuming point A has coordinates (x,y) and point B has coordinates (p,q).
// Also assuming the distance from B to C is delta. We want to find the 
// coordinates of C.

// I'll rename the coordinates for legibility.
double ax = x;
double ay = y;
double bx = p;
double by = q;

// this is what we want to find
double cx, cy;

// we need to establish a limit to acceptable computational precision
double epsilon = 0.000001;

if ( bx - ax  <  epsilon   &&   by - ay < epsilon ) {

  // the two points are too close to compute a reliable result
  // this is an error condition. handle the error here (throw
  // an exception or whatever).

} else {

  // compute the vector from B to A and its length
  double bax = bx - ax;
  double bay = by - ay;
  double balen = sqrt( pow(bax, 2) + pow(bay, 2) );

  // compute the vector from B to C (same direction of the vector from
  // B to A but with lenght delta)
  double bcx = bax * delta / balen;
  double bcy = bay * delta / balen;

  // and now add that vector to the vector OB (with O being the origin)
  // to find the solution
  cx = bx + bcx;
  cy = by + bcy;

}

您需要确保点A和B不太近,否则计算将不精确,结果将与您预期的不同.这就是epsilon应该执行的操作(您可能会或可能不想更改epsilon的值).

You need to make sure that points A and B are not too close or the computations will be imprecise and the result will be different than you expect. That's what epsilon is supposed to do (you may or may not want to change the value of epsilon).

理想地,epsilon的合适值与double中可表示的最小数字无关,而是与double为坐标值的量级提供的精度水平有关.

Ideally a suitable value for epsilon is not related to the smallest number representable in a double but to the level of precision that a double gives you for values in the order of magnitude of the coordinates.

我已经对epsilon进行了硬编码,这是一种通常用来定义其值的常用方法,正如您通常提前知道的数据量级一样,但是还有一些自适应"技术可以根据实际值来计算epsilon参数的值(在这种情况下,A和B的坐标以及增量).

I have hardcoded epsilon, which is a common way to define it's value as you generally know in advance the order of magnitude of your data, but there are also 'adaptive' techniques to compute an epsilon from the actual values of the arguments (the coordinates of A and B and the delta, in this case).

还请注意,我已经编写了易读性代码(无论如何,编译器都应该能够进行优化).随意重新编码.

Also note that I have coded for legibility (the compiler should be able to optimize anyway). Feel free to recode if you wish.

这篇关于如何找到直线上的点-目标c?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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