1

So I have a Node class that contains a member variable "center" that is a Vec2float*. The reason for this is because I want to use the drawSolidCircle function, and I need a Vec2float variable to represent the center. One of the questions I have is, is a Vec2float a vector, or a point in space? A lot of the member functions make it sound like some kind of vector class, yet the set() function only takes in two arguments which makes it seem like a point in space, and in order to draw a circle, you would need a point and a radius, not a vector. Also another problem I am having, is that if someone gives me 2 doubles, how can I convert them to Vec2float properly and set the x and y of center (if it even has an x and y). So for example in the function below, I am given an array of Entries and the length of it, 'n'. An entry has two member variables 'x' & 'y' which are both doubles. I want to create an array of Nodes and copy over that data to use it to draw circles.

cinder::Vec2<float>* center;//in my Node class

void function::build(Entry* c, int n) {
Node* nd = new Node[n];
for(int i = 0;i<n;i++) {
    nd[i].center = //what goes here if c[i].x and c[i].y are doubles?
}

references: Vec2 class: http://libcinder.org/docs/v0.8.4/classcinder_1_1_vec2.html list of functions that draw shapes, im using drawSolidCircle: http://libcinder.org/docs/v0.8.4/namespacecinder_1_1gl.html

Any suggestions?

2 Answers2

2

To make your life easy, you can use the cinder namespace. Add the following line after the includes at the top of your file.

using namespace ci;

which then enables you to simply write, for example:

Vec2f center(1.5f, 1.5f);
std::cout << center << std::endl;

Indeed, Vec2<float> is typedef'ed as Vec2f in Cinder.

Also, you shouldn't have to cast doubles into floats because they are casted implicitly, just pass them in!

Lastly, you really have to be careful with pointers. Most of the time, if I want an array of objects, I would use a std::vector, and use shared_ptr. Here's where I learned how to do just that: https://forum.libcinder.org/topic/efficiency-vector-of-pointers

I won't cover the whole theory behind vectors. Here's a good reference (using the Processing language): http://natureofcode.com/book/chapter-1-vectors/ In short, yes you should use them to store positions, but mathematically they are still vectors (you can think of a position vector as an arrow from the origin (0,0) to your current position).

I also suggest you have a look at the numerous samples provided with the library.

num3ric
  • 686
  • 5
  • 15
0

well i figured something out, it compiles for now, whether it will work for my program in the future is debatable. But here is what i did:

float f1 = (float)(c[i].x);
float f2 = (float)(c[i].y);
cinder::Vec2<float>* p = new cinder::Vec2<float>(f1,f2);
nd[i].center = p;

i casted the doubles to floats separately, then made a variable p using the Vec2 constructor, and then set center equal to that. like i said it compiles, we shall see if it works :D