having trouble connecting things in code? doesnt compile

I think I have most (if not all) the different elements I need but I'm having some difficulty connecting everything. It has to have two user entered points, show distance between them, polar coordinates, a rotate function that rotates the point by an angle, and determines if the points are on a certain line. How would you connect everything, or can you tell me what I'm doing wrong? Any help is appreciated :)

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

#include <iostream>
#include <cmath>

using namespace std;

class Distance
{
private:
	double x1, x2;  
	double y1, y2;  
	double dist;    

public:
	void set(double a1, double a2, double b1, double b2);   
	double get_dist()
	{
		return dist;
	}
	void rotate(double alpha);
	void polar(double x1, double y1, double& r, double& theta);

private:
	double calculate(); 
};

int main()
{
	Distance coordinates;  
	double x1, x2;  
	double y1, y2; 
	double dx, dy, slope, interc, px, py, left, top, right, bottom;

					
	cout << "Enter the first x  ";
	cin >> x1;

	cout << "Enter the first y  ";
	cin >> y1;

	cout << "Enter the second x  ";
	cin >> x2;

	cout << "Enter the second y  ";
	cin >> y2;
        
        cout << "Enter an angle: ";
        cin >> alpha;

	
	set(x1, x2, y1, y2);


	cout << "\n\nThe distance is " << get_dist() << endl;

	dx = x2 - x1;
	dy = y2 - y1;

	slope = dy / dx;
	interc = y1 - slope * x1; 

								
	if (x1 < x2)
	{
		left = x1;
		right = x2;
	}
	else
	{
		left = x2;
		right = x1;
	}
	if (y1 < y2)
	{
		top = y1;
		bottom = y2;
	}
	else
	{
		top = y1;
		bottom = y2;
	}
	if (slope * px + interc >(py - 0.01) &&
		slope * px + interc < (py + 0.01))
	{
		if (px >= left && px <= right &&
			py >= top && py <= bottom)
		{
			cout << "The point lies in the line\n";
		}
		else
			cout << "The point is outside the line\n";

	}
	else
		cout << "The point is outside the line\n";

}

system("pause");

	return 0;
}


void Distance::set(double a1, double a2, double b1, double b2)   
{
	a1 = x1;
	a2 = x2;
	b1 = y1;
	b2 = y2;


	dist = calculate();
}

double Distance::calculate()   
{
	return (sqrt((x2 - x1)*(x2 - x1) - (y2 - y1)*(y2 - y1)));
}

void Distance::rotate(double alpha) {
	x1 = x1 * cos(alpha) - y1 * sin(alpha);
	y1 = y1 * cos(alpha) + x1 * cos(alpha);
}
void Distance::polar(double x1, double y1, double& r, double& theta)
{
	r = sqrt((pow(x1, 2)) + (pow(y1, 2)));

	theta = atan(y1 / x1);

	theta = (theta * 180) / 3.141592654;
}
Last edited on
what do you mean by connect? Its not a graphics program, as far as I can tell?
sorry i should have been more clear. the code isnt a graphics program and it doesn't compile. I have all the different functions I need but im not sure if im passing values correctly and im trying to figure out how to fix it so it compiles and makes sense
ok. Its a bit to try to muddle through by hand.
don't suppose your compiler gave you an error message we could look at?
yeah it says identifier "set" is undefined, identifier "get_dist" is undefined, this declaration has no storage class or type specifier, syntax error: 'return'. and im using visual studio
Line 51 :
coordinates.set(x1, x2, y1, y2);

Line 54 :
cout << "\n\nThe distance is " << coordinates.get_dist() << endl;

I am assuming this is what you mean?
wow thanks mantorr22 that got rid of two of the errors
Here is the code that compiles and worked for me.

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
#include <iostream>
#include <cmath>

using namespace std;

class Distance
{
private:
	double x1, x2;  
	double y1, y2;  
	double dist;    

public:
	void set(double a1, double a2, double b1, double b2);   
	double get_dist()
	{
		return dist;
	}
	void rotate(double alpha);
	void polar(double x1, double y1, double& r, double& theta);

private:
	double calculate(); 
};

int main()
{
	Distance coordinates;  
	double x1, x2;  
	double y1, y2; 
	double dx, dy, slope, interc, px, py, left, top, right, bottom, alpha;

					
	cout << "Enter the first x : ";
	cin >> x1;

	cout << "Enter the first y : ";
	cin >> y1;

	cout << "Enter the second x : ";
	cin >> x2;

	cout << "Enter the second y : ";
	cin >> y2;
        
	cout << "Enter an angle : ";
	cin >> alpha;
	
	coordinates.set(x1, x2, y1, y2);

	cout << "\n\nThe distance is " << coordinates.get_dist() << endl;

	dx = x2 - x1;
	dy = y2 - y1;

	slope = dy / dx;
	interc = y1 - slope * x1; 

								
	if (x1 < x2)
	{
		left = x1;
		right = x2;
	}
	else
	{
		left = x2;
		right = x1;
	}

	if (y1 < y2)
	{
		top = y1;
		bottom = y2;
	}
	else
	{
		top = y1;
		bottom = y2;
	}

	if (slope * px + interc >(py - 0.01) &&
		slope * px + interc < (py + 0.01))
	{
		if (px >= left && px <= right &&
			py >= top && py <= bottom)
		{
			cout << "The point lies in the line\n";
		}
		else
			cout << "The point is outside the line\n";
	}
	else
	{
		cout << "The point is outside the line\n";
	}

	system("pause");
	return 0;
}

void Distance::set(double a1, double a2, double b1, double b2)   
{
	x1 = a1;
	x2 = a2; 
	y1 = b1;
	y2 = b2;

	dist = calculate();
}

double Distance::calculate()   
{
	return (sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)));
}

void Distance::rotate(double alpha) 
{
	x1 = x1 * cos(alpha) - y1 * sin(alpha);
	y1 = y1 * cos(alpha) + x1 * cos(alpha);
}

void Distance::polar(double x1, double y1, double& r, double& theta)
{
	r = sqrt((pow(x1, 2)) + (pow(y1, 2)));

	theta = atan(y1 / x1);

	theta = (theta * 180) / 3.141592654;
}


Line 108 - 111 : You are assigning the wrong targets.
Line 119 : You are applying the wrong formula for calculating distance.

The formula should be √((x2 - x1)² + (y2 - y1)²)
Edit: I didn't see the last 4 posts, but what I say here still applies :+)

If you have compiler warnings / errors , you should post them here verbatim.
Here is the compiler output, using cpp.sh (the gear icon top right of the code), with all 3 warnings on:

In function 'int main()':
48:16: error: 'alpha' was not declared in this scope
51:20: error: 'set' was not declared in this scope
54:45: error: 'get_dist' was not declared in this scope
29:11: warning: unused variable 'coordinates' [-Wunused-variable]
At global scope:
100:7: error: expected constructor, destructor, or type conversion before '(' token
102:2: error: expected unqualified-id before 'return'
103:1: error: expected declaration before '}' token


But before that, I would change a number of things:

Make a Point a class. Now a Line class has 2 Points as members.

Distance can now be a function which takes 2 const Point& as parameters.

Rotate and Move can also be functions which take a Point as an argument.

To call a member function, one needs to do it via an object, as in:

cout << "\n\nThe distance is " << coordinates.get_dist() << endl;

Use a member initialization list in constructors, instead of a set function.

Good Luck !!
Last edited on
Also, pi is a constant as is 180.0/ pi, so make it so:

1
2
constexpr double pi = std::acos(-1.0);
constexpr double DegToRad = 180.0 / pi;
Another thing:

Always declare 1 variable per line, it's best practise.
So the following is a function, not a whole lot of code in main:

1
2
3
bool IsPointOnLine(const Line& TheLine, const Point& ThePoint) {

}
Last edited on
@TheIdeasMan : You should edit your post instead of making multiple posts. That means your post will be more productive.
1 per line as a best practice is debatable. This makes a large mess in some variable intense code, for no reason. Use common sense, like any other style decisions, on what looks best.

grouping related items, like your coordinates, is readable and clean:

double x1, y1, z1; //3d point, 3 related variables, this seems clean and OK.

double pi, e, number_of_something, temp, usrinput, x; //not ok. a bunch of random nonsense jumbled together. the space saved isn't improving the ability to read or understand the code.



jonnin wrote:
1 per line as a best practice is debatable.


Yes, I see your point. But maybe a matter of personal preference. However I do tend to listen to these guys:

https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines.html#Res-name-one

Their example didn't cover your methodology though.

Also, in a class declaration one can set default values:

1
2
3
4
5
class Point {
  double x = 0.0; // comments can be used to specify expected range of values
 double y = 0.0;  // these might differ for these 3 variables
 double z = 0.0;
};


This is the same as if a member initialisation list had been used with a constructor.
Last edited on
wow thanks everyone for all the advice!!! i really appreciate it :)
Topic archived. No new replies allowed.