Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>OK, I've done this exact same thing in my own app.</p> <p>The trick I did to make it easier was to have a function to calculate the "unitVector" of the line.</p> <p>i.e. the vector change in the line based on a line length of 1.</p> <p>It just uses simple pythagorus...</p> <pre><code>- (CGSize)unitVectorFromPoint:(CGPoint)start toPoint:(CGPoint)end { //distance between start an end float dX = end.x - start.x; float dY = end.y - start.y; float distance = sqrtf(dX * dX + dY * dY); // simple pythagorus //unit vector is just the difference divided by the distance CGSize unitVector = CGSizeMake(dX/distance, dY/distance); return unitVector; } </code></pre> <p>Note... it doesn't matter which way round the start and end are as squaring the numbers will only give positive values.</p> <p>Now you can use this vector to get to any point along the line between the two points (centre of the circle and target).</p> <p>So, the start of the line is ...</p> <pre><code>CGPoint center = // center of circle CGPoint target = // target float radius = //radius of circle float dX = center.x - target.x; float dY = center.y - target.y; float distance = sqrtf(dX * dX + dY * dY); CGSize unitVector = [self unitVectorFromPoint:center toPoint:target]; CGPoint startOfLaser = CGPointMake(center.x + unitVector.x * radius, center.y + unitVector.y * radius). CGPoint midPointOfLaser = CGPointMake(center.x + unitVecotr.x * distance * 0.5, center.y + unitVector.y * distance * 0.5); </code></pre> <p>This just multiplies the unit vector by how far you want to go (radius) to get to the point on the line at that distance.</p> <p>Hope this helps :D</p> <p>If you want the mid point between the two points then you just need to change "radius" to be the distance that you want to calculate and it will give you the mid point. (and so on).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload