Friday, October 10, 2008

A Couple More Math Snippets for 2D Graphics

Every programmer has a collection of general purpose libraries and classes that they re-use. I've got a rather extensive collection of Objective-C snippets from over the years. I periodically blow the dust off of them and use them in iPhone applications. Here's one I dug out a few weeks ago for one of the sample projects in the book.

These three functions are helpful when doing 2D graphics, either using Quartz / Core Graphics, or Open GL (but don't forget to flip the Y-axis for the latter). The first function will return the distance between two points on a 2-dimensional plane, the second will give you the absolute angle of a line formed by two points, and the third will return the angle between two lines,


#include "CGPointUtils.h"
#include <math.h>
#import "Constants.h"


CGFloat distanceBetweenPoints (CGPoint first, CGPoint second) {
CGFloat deltaX = second.x - first.x;
CGFloat deltaY = second.y - first.y;
return sqrt(deltaX*deltaX + deltaY*deltaY );
};
CGFloat angleBetweenPoints(CGPoint first, CGPoint second) {
CGFloat height = second.y - first.y;
CGFloat width = first.x - second.x;
CGFloat rads = atan(height/width);
return radiansToDegrees(rads);
//degs = degrees(atan((top - bottom)/(right - left)))
}
CGFloat angleBetweenLines(CGPoint line1Start, CGPoint line1End, CGPoint line2Start, CGPoint line2End) {

CGFloat a = line1End.x - line1Start.x;
CGFloat b = line1End.y - line1Start.y;
CGFloat c = line2End.x - line2Start.x;
CGFloat d = line2End.y - line2Start.y;

CGFloat rads = acos(((a*c) + (b*d)) / ((sqrt(a*a + b*b)) * (sqrt(c*c + d*d))));

return radiansToDegrees(rads);
}




3 comments:

Dan said...

I just ran across your example because I was surprised to get a compile error when adding or subtracting two CGPoints together. I find it strange that standard operators aren't implemented for them... but I guess they are structs instead of classes. Anyhow, is there any better way to do math on CGPoints besides just handling the x/y components separately?

Jaanus said...

I wanted a version of angleBetweenLines that would also have the sign. Your version always returns positive, but sometimes you need the direction of rotation of a line from its original position. See this thread on StackOverflow for a signed version.

h4ns said...

What youre saying is completely true. I know that everybody must say the same thing, but I just think that you put it in a way that everyone can understand. I also love the images you put in here. They fit so well with what youre trying to say. Im sure youll reach so many people with what youve got to say.

Arsenal vs Huddersfield Town live streaming
Arsenal vs Huddersfield Town live streaming
Wolverhampton Wanderers vs Stoke City Live Streaming
Wolverhampton Wanderers vs Stoke City Live Streaming
Notts County vs Manchester City Live Streaming
Notts County vs Manchester City Live Streaming
Bologna vs AS Roma Live Streaming
Bologna vs AS Roma Live Streaming
Juventus vs Udinese Live Streaming
Juventus vs Udinese Live Streaming
Napoli vs Sampdoria Live Streaming
Napoli vs Sampdoria Live Streaming
Fulham vs Tottenham Hotspur Live Streaming
Fulham vs Tottenham Hotspur Live Streaming
AS Monaco vs Marseille Live Streaming
AS Monaco vs Marseille Live Streaming
Alajuelense vs Perez Zeledon Live Streaming
Alajuelense vs Perez Zeledon Live Streaming
Technology News | News Today | Live Streaming TV Channels