How can I find the points in a line - Objective c?

僤鯓⒐⒋嵵緔 提交于 2019-12-12 20:04:58

问题


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

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?

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

Refer this image..

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.

Thank you


回答1:


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.

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).

Edit:

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:

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;

}

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).

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.

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.




回答2:


It's not so hard, translate your segment into a math line expression, x pixels may be translated into radius of a circe with center in B, make a system to find where they intercept, you get two solutions, take the point that is closer to A.




回答3:


This is the code you can use

 float distanceFromPx2toP3 = 1300.0;    

 float mag = sqrt(pow((px2.x - px1.x),2) + pow((px2.y - px1.y),2));
 float P3x = px2.x + distanceFromPx2toP3 * (px2.x - px1.x) / mag;
 float P3y = px2.y + distanceFromPx2toP3 * (px2.y - px1.y) / mag;

 CGPoint  P3 = CGPointMake(P3x, P3y);

Either you can follow this link also it will give you the detail description -

How to find a third point using two other points and their angle.

You can find out number of points whichever you want to find.



来源:https://stackoverflow.com/questions/12617387/how-can-i-find-the-points-in-a-line-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!