i need to calculate the distance between two CGPoint
s. I refered this and this, but I don't get it.
Learn how to find the distance between two points by using the distance formula, which is an application of the Pythagorean theorem. We can rewrite the Pythagorean theorem as d=√((x_2-x_1)²+(y_2-y_1)²) to find the distance between any two points.
This means that you will square the x-axis distance (x2 - x1), and that you will separately square the y-axis distance (y2 - y1). Add the squared values together. This will give you the square of the diagonal, linear distance between your two points.
Well, with stuff your refering too where is the full code:
CGPoint p2; //[1] CGPoint p1; //Assign the coord of p2 and p1... //End Assign... CGFloat xDist = (p2.x - p1.x); //[2] CGFloat yDist = (p2.y - p1.y); //[3] CGFloat distance = sqrt((xDist * xDist) + (yDist * yDist)); //[4]
The distance is the variable distance.
What is going on here:
You can't really make a CGPoint be the distance, distance doesn't have an x and y component. It is just 1 number.
If you think CGPoint is a unit of measurement (for example feet is a unit of measurement) it is not.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With