Displaying Points

Apr 14, 2014 at 5:59am
Im still a bit new to pointers and structs, and i dont understand why my display points function isnt working. Im thinking what im returning in readPoints isnt working right but it doesnt make much sense for the display to not work at all. Any suggestions would be greatly appriciated.

here is read points
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
Points* readPoints(const char* file_name)
{
	ifstream input_file;
	input_file.open(file_name);
	
	int num_points;
	input_file >> num_points;
	
	Point** point_array = new Point*[num_points];
	
	for(int i = 0; i < num_points; i++)
	{
		double x;
		input_file >> x;
		double y;
		input_file >> y;
		double z;
		input_file >> z;
		
		Point* point = createPoint(x, y, z);
		point_array[i] = point;
	}
	
    Points* points = new Points;
    points->points = point_array;
	input_file.close();
	
	return points;
}


and display ponts
1
2
3
4
5
6
7
8
9
10
11
12
void displayPoints(const Points* points)
{
	Point** points_array = points->points;
	int num_points = points->num_points;
	
	for(int i = 0; i < num_points; i++)
	{
		Point* point = points_array[i];
		cout<< point <<endl;
		displayPoint(point);
	}
}



and here are the other functions that i use
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Point* createPoint(double x, double y, double z)
{
	Point* point = new Point;
	point->x = x;
	point->y = y;
	point->z = z;
	
	return point;
}

void displayPoint(const Point* const point)
{
	cout << setprecision(3) <<fixed;
	cout<< "(" << point->x << ", " << point->y << ", " << point->z << ")" <<endl;
}
Apr 14, 2014 at 6:48am
Aside from unneeded indirection in many places, you do not assigning anything to points->num_points in readPoints function.
Apr 14, 2014 at 7:47am
Thank you! its displaying it now. Would you have any idea of how to sort points from a reference number?
Apr 14, 2014 at 7:53am
how to sort points from a reference number?
I am not sure I understand you.

How points should be ordered?
Apr 14, 2014 at 7:58am
I have to sort points (x, y, z) from the distance of the reference point
Apr 14, 2014 at 8:31am
Either use handcrafted sorting function or use library provided instruments: http://ideone.com/04EpZ4

Adapt to your case.
Apr 14, 2014 at 3:54pm
I know i need to make my own sorting function, i just dont understand how to sort points from a reference point. Would i need to determine the distance from refpoint with all the points then determine which distance is greater?
Apr 14, 2014 at 5:50pm
You will need a comparsion function which will return if first point is closer to reference point than second one. Then you can just pass comparsion function to std::sort alghorithm

You can make comparsion function in two ways:
1) if ref point is known at compile time, you can just hardcode it to your function.
2) Otherwise you should pass it as a parameter to your function and then bind real reference point to your function (as I done in my example).
3) Use lambdas.
Topic archived. No new replies allowed.