Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

yaazz

macrumors newbie
Original poster
Jan 23, 2009
2
0
Hey there, I am making a vector class in objective C and would like to make it as expandable as possible.
I would like to have a 2D version of the class, and a 3D version, and have the two classes able to work with each other interchangably. The only difference is that the 3d version will have functions to deal with cross products and stuff like that.

To do this, I made a protocol with all of the basic functions a vector would need. Then, I will just add additional classes into the interface for Vector3d.

But I can't figure out how to specify that I want the protocol to work with anything that implements the protocol.
I can explain better by just putting the protocol here
Code:
@protocol vecProt

+createAtX:Y:Z:;	//class variable to create a vector at <X,Y,Z>
+add:To:;			//class variable to add two vectors
+subtract:From:;	//class variable to subtract 2 vectors

-(id) init;				
-(id) add:;			//adds this vector to another
-(id) subtract:;		//subtracts this vector from another
-(id) dotProductWith:;//returns te dot product of this vector with another
-(id) length;		//Returns the length of the vector
-(id) lengthSquared;	//Returns the Squared Length of the vector
@end

right now I am returning id, but would like to say something along the lines of "return a vector", how can I do this exactly?

also, how do I specify what I can add and subtract from after the ":" in the method names?
 

kpua

macrumors 6502
Jul 25, 2006
294
0
Code:
id <vecProt> aVector

is how you declare a variable conforming to the protocol.

So, your add method should look like this:

Code:
-(id) add:(id <vecProt>)other;

However, I don't think this is the best design in the world. In every implementation of these methods, you'll have to deal with what happens when you add a 2D vector to a 3D vector, or vice-versa.

(Also, unless you're using another object to represent length, (id) isn't the right return type for those methods)
 

yaazz

macrumors newbie
Original poster
Jan 23, 2009
2
0
Yea im not a fan either but there is extra credit for creating a vector2d class that interacts with vector 3d.
I suppose I could just derive vec3d from vec2d without the protocol
 

kpua

macrumors 6502
Jul 25, 2006
294
0
That is an option, but then do you subclass vec3d when you need vec4d?

Why not just create a single N-dimensional vector class?
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,566
Yea im not a fan either but there is extra credit for creating a vector2d class that interacts with vector 3d.
I suppose I could just derive vec3d from vec2d without the protocol

Protocols and subclasses should only be used in the right situations. You would use a protocol if the operations are actually the same. If one operation is adding a 2d vector, and another operation is adding a 3d vector, then they are not the same operation, and you shouldn't use a protocol.

Same with subclassing: A 3d vector is not a 2d vector. In C++, you can use templates to define multiple, similar classes, but not in Objective-C.

If this is for a programming class, you should demonstrate to your teacher that you learned one of the most important aspects of programming: When requirements are not clear, go back and check what the requirements are. So ask your teacher exactly what interaction between 2d and 3d she or he wants. It may make sense in some cases to treat a 2d vector as if it were a 3d vector with z = 0. In that case the easiest way is to duplicate the code that for example adds a 3d vector to a 3d vector and change it so it adds 2d to 3d, assuming the last coordinate is zero.

On the other hand, adding a method to the 2d vector class that returns a new 3d object with the same first two coordinates might be just as useful.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.