2

I decided to create a Vector Class for my app in strict objective-C. The way i thought this could happen is to create a Point2D class first holding each point in a (x,y) format and then create a Vector2D class holding 2 points.

Where the whole thing got stuck is in my Vector2D class. Im trying to find a way to hold 2 objects (actually 2 x Point2D objects) in an instance variable in my Vector2D class. I thought of an NSMutableArray but i recall many problems in this situation, plus im not sure is the most efficient solution since im dealing with floats.

What i would like is some sort of guidance through a Vector class for objective-c or your tips/recommendations on my "quest". How would you choose to do such a thing and also what would require extra attention.

4
  • 1
    "create a Vector2D class holding 2 points" That doesn't make sense. 2D vectors do not contain two 2D points. They contain 2 values, just like a point. A "point" is just a vector that refers to a position. They have semantic differences, but they can contain the same data. Commented Jan 25, 2012 at 14:49
  • doesn't in theory a vector has a (x,y) starting point and an ending (x,y) point (strictly 2d speaking)? Otherwise how can direction be called in a vector? Please correct me if im wrong. I could see a pure (x,y) "vector" class holding just shape edges, thats why i also asked for suggestions. what would your suggestion be for an objective-c vector class? thank you for your answer Commented Jan 25, 2012 at 16:27
  • 3
    That's a ordered line segment. There are many things called "vector". Position vectors, gradient vectors, etc. All of them contain different things. In terms of rendering and graphics, a vector type is a position or direction, depending on its use. You generally don't want separate types for these kinds of things; you don't want to have to write separate matrix/position and matrix/direction multiplication functions. So most vector math libraries simply think of a vector as an N-dimensional array, which can be interpreted as a position or direction. Commented Jan 25, 2012 at 16:34
  • i get your point and i can definitively see the code-efficient side of this perspective. And i gotta tell you i love everything thats code-efficient. So modifying my initial plan:: Having a Point2D class storing (x,y) floats and filling an object's nsmutablearray with Point2Ds would save me from an extra class and prolly one or two more nsmutablearrays inside objects plus all the unnecessary variables during this process. I think thats the plan im sticking so far. (edit) - I wish you had chose to answer though so i could vote you up Commented Jan 25, 2012 at 16:55

1 Answer 1

1

C array of fixed size (2 in your case) should be perfectly acceptable in your situation. Since your vector does not grow dynamically, I think this should work just fine:

@interface Vector2D : NSObject {
    Point2D *points[2];
}
// Properties and methods
@end
Sign up to request clarification or add additional context in comments.

3 Comments

true, a vector will only hold 2 values, but those vectors are created mostly for shapes so different shapes require different vector count. Do you think i should go ahead with another object holding all the Vector2D objects for a shape? -And is this really efficient? thanks for your answer! Also i would like best if i stay away from C or C++ implementations and stick to objective-c
also i cant form it as this? Xcode pops this should be prolly be an array of pointers. so im obliged to follow the Point2D *points[2]; format.. doesnt this make it more complicated?
@Bisder I did not realize your Point2D is an objective C object too. I edited the answer to mark it as a pointer. As far as using multiple instances of Vector2D in shapes, I would use NSArray/NSMutableArray to hold multiple Vector2D. Using NSArray/NSMutableArray in situations when you always have exactly two objects is wasteful, but in situations when the number of points is not known at compile time it makes perfect sense.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.