ForeverNights
ForeverNights

Reputation: 631

Objective C Iphone Development Distance Formula

Basically I need to calculate the distance between a point and a line (which has two known coordinates on) in XY axis I got the algorithm but I don't know how to implement it into ObjC.

Illustration: I have a line that pass through two points A(100,300) and B(200,100)

m = (y-y1)/x(x-x1) = 300-100 / 100 - 200 = -2

so:

-2(x-x1) = (y-y1)

Replace x and y we have:

-2(100-x1) = 200 - y1 <=> -2x + y - 500 = 0 (Line equation that passes through above 2 points)

And the use another formula to calculate the distance:

ax + by + c / sqrt(a^2 + b^2)

Replace the x and y of point I want to calculate the distance to C(10,20)

-2*1000 + 2000 - 500 / sqrt (100 + 400) = distance I wanna find

Simple this is it, but how do i do this in ObjC?

Okey this is the basically what I have done, and dont know how to continue:

float coordinateXStart;
float coordinateYStart;
float coordinateXEnd;
float coordinateYEnd;

coordinateXStart = [[strokesArray objectAtIndex:strokeNo] startX];
coordinateYStart = [[strokesArray objectAtIndex:strokeNo] startY];
coordinateXEnd = [[strokesArray objectAtIndex:strokeNo] endX];
coordinateYEnd = [[strokesArray objectAtIndex:strokeNo] endY];

//let's rock on distance formula from point to line
float m = (coordinateYEnd-coordinateYStart)/(coordinateXEnd-coordinateXStart);


enter code here

Blockquote Okey so I had m....but how to automate the calculation of a,b and c of ax + by + c of line equation?

Upvotes: 0

Views: 708

Answers (2)

Kai
Kai

Reputation: 511

And after you know the C Math Library you should continue with The Objective-C Programming Language

Upvotes: 1

Jason Whitehorn
Jason Whitehorn

Reputation: 13685

You said -2*1000 + 2000 - 500 / sqrt (100 + 400) = distance I wanna find, and why not just write that in code?

The statement:

distance = -2*1000 + 2000 - 500 / sqrt (100 + 400)

Is perfectly valid C (fragment), and therefore Objective-C, just make sure you consume the C Math library first

Upvotes: 1

Related Questions