problem with pointers

I want to implement the mutual-avoidance iteration with c++.
The method consists to generate random points in the plane in order to obtain a point cloud. Then move the points to improve the arrangement of points in the plane.
I create a point class and a distance function with points as argument
i.e. double dist(point p1, point p2).
The problem:

when I define my point like this: point *p1, *p2;
for(...){
p1 = &speckle[];
p2 = &speckle[];
}
I can't pass p1 and p2 as argument in distance function.
can someone tell me why?
thank you in advance.

here is my program code:

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
 #include "stdafx.h"
 #include <iostream>
 #include <fstream>
 #include <vector>



#define MAXSPECKLE 10000

double frand(double fmin, double fmax);

class Point
{
public:
	double x;
	double y;

	static double fdist(Point p1, Point p2)
	{

		double dx;
		double dy;

		dx = p1.x - p2.x;
		if (dx > 0.5) dx -= 1.0;
		else if (dx < -0.5) dx += 1.0;

		dy = p1.y - p2.y;
		if (dy > 0.5) dy -= 1.0;
		else if (dy < -0.5) dy += 1.0;

		return sqrt(dx * dx + dy * dy);
	}

};


double frand(double fmin, double fmax)
{
	double f = (double)rand() / RAND_MAX;
	return fmin + f * (fmax - fmin);
}


int main()
{
	int npoints;
	double d;
	double min_d;
	double mindist;
	double avgmindist;
	double v_mindist;

	Point speckle[MAXSPECKLE];
	Point *p1;
	Point *p2;

	std::vector<Point> vs(2);
	std::vector<Point> v(2);

	double dv;

	avgmindist = 0.;
	min_d = 0.0;
	d = 0.0;

	double effectradius;

	std::cout << "number of points: ";
	std::cin >> npoints;
	std::cout << "\n";

	for (int i = 0; i < npoints; i++)
	{

		speckle[i].x = frand(0, 1);
		speckle[i].y = frand(0, 1);

		//std::cout << speckle[i].x << "\t" << speckle[i].y << std::endl;
		effectradius = 2.5 * sqrt(2 / (npoints * sqrt(3)));

		p1 = &speckle[i];

		vs[i] = {0., 0.};

		mindist = 1;

		for (int j = 0; j < npoints; j++)
		{
			if (j == i) continue;
			p2 = &speckle[j];

			d = Point::fdist(p1, p2);

			if (d > effectradius) continue;
			if (d < mindist)
				min_d = d;

			vs[j] += (*p1 - *p2) / (d * d);
		}

		v[i] = vs[i];


		dv = sizeof(vs);

		v_mindist = mindist;

		avgmindist += mindist;

	}

	avgmindist /= npoints;

	double dvs;
	double factor;

	dvs = 0.;

	for (int i = 0; i < npoints; i++)
	{
		p1 = &speckle[i];
		dvs += dv;
	}

	dvs /= npoints;

	if (dvs == 0)
	{
		factor = 1;
	}
	else
	{
		factor = (effectradius * 0.04) / dvs;
	}


	for (int i = 0; i < npoints; i++)
	{
		p1 = &speckle[i];
		p1 += factor * v[i]; // move the point

		if (p1->x < 0.0)
			p1->x += 1.0;
		else if (p1->x >= 1.0)
			p1->x -= 1.0;
		if (p1->y < 0.0)
			p1->y += 1.0;
		else if (p1->y >= 1.0)
			p1->y -= 1.0;

		std::cout << speckle[i].x << "\t" << speckle[i].y << std::endl;


	}


	std::cout << "\n" << std::endl;
	system("pause");

	return 0;
}



Last edited on
1
2
3
4
5
6
Point *p1;
Point *p2;

// Then, much later

d = Point::fdist(p1, p2);
You're trying to pass p1, which is a pointer-to-a-Point, and p2, which is a pointer-to-a-Point.

The function parameters are expect to be objects of type Point. Not pointer-to-a-Point:
static double fdist(Point p1, Point p2)

The function expects the parameter to be of type Point. You're trying to pass it something that is not a Point.
Last edited on
thank you for your answer.
speckle[MAXSPECKLE], *p1 and *p2 are objects of type point.
1
2
3
4
5
6

for(int i; i< n; i++)
{
p1 = &speckle[i] // p1 address of speckle[i]
p2 = &speckle[i] // p2 address of speckle[i]
}


can you please take a loot at this great work.

http://relativisticobserver.blogspot.com/2012/01/texture-part-1.html

I think it works.
Yes, that code works,. Your code doesn't.

You wrote a function that takes Point objects, and you're trying to pass pointer-to-Point.
It's the same code I think there is a typo somewhere in that code.
I told you what the mistake was already. if you copied that code exactly, then the same mistake is in that code. Fix your code.
The code from the relativisticobserver page has the same error that you are displaying. The types of the arguments passed into fdist do not match the types from the function definition.

I think we found an error in the internet.
Topic archived. No new replies allowed.