Line Class Malfunction

Hi there. I'm programming this project called 'Canvas', in which I'm practicing and implementing Classes, which is what are giving me on the University. This 'Class Line' is supposed to draw a line, using functions and services from 'Class Point' which is one we made in class. The problem is that, it compiles perfectly but, when plotting the points and drawing the line, it gives me 2 weird results: one is that I enter coordinates for an horizontal line,which the program draws like a vertical line wherever the program wants. The second weird result is that, when I enter the coordinates for a vertical or diagonal line, the line is drawn in little segments, and wherever the program wants. I've been trying to resolve this since 4:30PM of today, and I haven't got to the solution. Any help?

Here are the 2 Clases, Line and Point, and my 'Driver'(main), and also a link to a Print Screen of the results that I'm getting.

http://img22.imageshack.us/img22/1240/prtscp.png

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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#pragma once
#include "Point.h"
class Line
{
private:
	Point initial;
	Point final;
	char symbol;
	double x1,y1,x2,y2;
public:
	Line(double x1=0.0,double y1=0.0,double x2=4.0,double y2=4.0,char symbol='+');
	Line(const Point & initial,const Point & final);
	Line(const Line & oneLine);
	~Line();
	Line & operator =(const Line & oneLine);
	const Point & getInitialPoint()const;
	const Point & getFinalPoint()const;
	void changeInitialPoint(const Point & initial);
	void changeFinalPoint(const Point & final);
	double Long()const;
	void askExtremes();
	bool isHorizontal()const;
	bool isVertical()const;
	bool isDiagonal()const;
	bool hasSlope()const;
	friend ostream & operator<<(ostream & out,Line & oneLine);
	bool Include(double x,double y)const;
	void Show()const;
	double M()const;
	double B()const;
	bool inRangeHorizontal(double x,double y)const;
	bool inRangeVertical(double x,double y)const;
	bool inRangeDiagonal(double x,double y)const;
	bool inRangeX(double x)const;
	bool inRangeY(double y)const;
};

#include "Line.h"

Line::Line(double x1, double y1, double x2, double y2, char symbol):initial(x1,y1),final(x2,y2)
{}
Line::Line(const Point &initial, const Point &final):initial(initial),final(final)
{}
Line::Line(const Line &oneLine):initial(oneLine.initial),final(oneLine.final)
{
	this->symbol=symbol;
}
Line::~Line()
{}
Line & Line::operator =(const Line &oneLine)
{
	(this->initial)=(oneLine.initial);
	(this->final)=(oneLine.final);
	(this->symbol)=(oneLine.symbol);
	return(*this);
}
const Point & Line::getInitialPoint() const
{
	return(this->initial);
}
const Point & Line::getFinalPoint() const
{
	return(this->final);
}
void Line::changeInitialPoint(const Point &initial)
{
	(this->initial).changeX(initial.getX());
	(this->initial).changeY(initial.getY());
}
void Line::changeFinalPoint(const Point &final)
{
	(this->final).changeX(final.getX());
	(this->final).changeY(final.getY());
}
double Line::Long() const
{
	return((this->initial).distance(this->final));
}
bool Line::isHorizontal() const
{
	return((this->initial).getY()==(this->final).getY());
}
bool Line::isVertical() const
{
	return((this->initial).getX()==(this->final).getX());
}
bool Line::isDiagonal() const
{
	return(!(this->isVertical()||this->isHorizontal()));
}
bool Line::hasSlope() const
{
	return(!(this->isVertical()));
}
void Line::askExtremes()
{
	cout<<"Enter the initial point: "<<endl;
	(this->initial).askCoordinates();
	cout<<"Enter the final point: "<<endl;
	(this->final).askCoordinates();
}
ostream & operator<<(ostream & out,Line & oneLine)
{
	out<<"Initial Point: ";
	out<<(oneLine.initial);
	out<<endl<<"Final Point: ";
	out<<(oneLine.final);
	if(oneLine.isHorizontal())
	{
		out<<endl<<"The line is horizontal";
	}
	else
	{
		if(oneLine.isVertical())
		{
			out<<endl<<"The line is vartical";
		}
	}
	if(oneLine.hasSlope())
	{
		out<<endl<<"The line has slope";
	}
	else
	{
		out<<endl<<"The line has indefinite slope";
	}
	return(out);
}
bool Line::Include(double x,double y)const
{
	bool Is;
	if(this->isHorizontal())
	{
		Is=this->inRangeHorizontal(x,y);
	}
	else
	{
		if(this->isVertical())
		{
			Is=this->inRangeVertical(x,y);
		}
		else
		{
			if(this->isDiagonal())
			{
				Is=this->inRangeDiagonal(x,y);
			}
			else
			{
				Is=false;
			}
		}
	}
	return(Is);
}
void Line::Show() const
{
	cout<<this->symbol<<endl;
}
double Line::M()const
{
	double dx,dy;

	dy=this->final.getY()-this->initial.getY();
	dx=this->final.getX()-this->initial.getX();

	return (dy/dx);
}

double Line::B()const
{
	return(this->final.getY()-(this->M()*this->final.getX()));
}
bool Line::inRangeHorizontal(double x,double y)const
{
	return((this->initial.getY()==y)&&(((this->initial.getX()<=x)&&this->final.getX()>=x)||(this->final.getX()<=x&&this->initial.getX()>=x)));
}
bool Line::inRangeVertical(double x,double y)const
{
	return((this->initial.getX()==x)&&(((this->initial.getY()<=y)&&this->final.getY()>=y)||(this->final.getY()<=y&&this->initial.getY()>=y)));
}
bool Line::inRangeDiagonal(double x,double y)const
{
	return((this->inRangeX(x)&&this->inRangeY(y))&&(y==(this->M()*x+this->B())));
}
bool Line::inRangeX(double x) const
{
	return((this->initial.getX()<=x&&this->final.getX()>=x)||(this->final.getX()<=x&&this->initial.getX()>=x));
}
bool Line::inRangeY(double y) const
{
	return((this->initial.getY()<=y&&this->final.getY()>=y)||(this->final.getY()<=y&&this->initial.getY()>=y));
}

#pragma once
#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;
class Point
{
private :
	double x;
	double y;
	char symbol;
	bool Is;
public:
	Point(double x=0.0,double y=0.0,char symbol='*');
	Point(const Point & onePoint);
    ~Point();
	Point & operator =(const Point & onePoint);
	double getX()const ; 
	double getY()const;
	void changeX(double x);
	void changeY(double y);
	double distance(const Point & onePoint)const;
	void askCoordinates();
	friend ostream & operator<<(ostream & output,Point & onePoint);
	bool include(double x,double y)const;
	void show()const;
};

#include "Point.h"
#include <iostream>
#include <cstring>
#include <cmath>

Point::Point(double x, double y,char symbol)
{
	this->x=x;
	this->y=y;
	this->symbol=symbol;
}
Point::Point(const Point & onePoint)
{
	this->x=onePoint.x;
	this->y=onePoint.y;
	this->symbol=onePoint.symbol;
}
Point::~Point()
{
}
Point & Point::operator =(const Point & onePoint)
{
	this->x=onePoint.x;
	this->y=onePoint.y;
	this->symbol=onePoint.symbol;
	return(*this);
}
double Point::getX() const
{
	return(this->x);
}
double Point::getY() const
{
	return(this->y);
}
void Point::changeX(double x)
{
	this->x=x;
}
void Point::changeY(double y)
{
	this->y=y;
}
double Point::distance(const Point & onePoint) const
{
	return(sqrt((pow(this->x-onePoint.x,2))+(pow(this->y-onePoint.y,2))));
}
void Point::askCoordinates()
{
	cout<<"Enter the new 'X' coordinate: ";
	cin>>this->x;
	cout<<"Enter the new 'Y' coordinate: ";
	cin>>this->y;
}
ostream & operator<<(ostream & output,Point & onePoint)
{
	output<<"("<<onePoint.x<<","<<onePoint.y<<")";
	return(output);
}
bool Point::include(double x, double y) const
{
	return((this->x==x)&&(this->y==y));
}
void Point::show() const
{
	cout<<this->symbol<<endl;
}

#include "Line.h"
#include "Point.h"
#include <iostream>
using namespace std;

int main()
{
	Point p1;
	Point p2;
	Point p3;
	Line l1;
	Line l2;
	//Line l3;

	//p1.askCoordinates();
	//p2.askCoordinates();
	//p3.askCoordinates();
	l1.askExtremes();
	l2.askExtremes();

	for(int y=0;y<=40;y++)
	{
		for(int x=0;x<=40;x++)
		{
			if(p1.include(x,y))
			{
				p1.show();
			}
			else
			{
				if(p2.include(x,y))
				{
					p2.show();
				}
				else
				{
					if(p3.include(x,y))
					{
						p3.show();
					}
					else
					{
						if(l1.Include(x,y))
						{
							l1.Show();
						}
						else
						{
							if(l2.Include(x,y))
							{
								l2.Show();
							}
							else
							{
								cout<<" ";
							}
						}
					}
				}
			}
		}
		cout<<endl;
	}
}

I've got to the conclusion that there's is something in the return of Line::inRangeVertical, but I have no clue of what I'm supposed to change, to repair...
You can't compare floating point numbers for equality like you are doing in many places.

The best you can do is compare for equality within some epsilon.

As a side note, your code is very hard to read for two reasons.

First, no spaces.

Second, repeated use of this-> for everything.

I'm sorry, my english is not the best, and my c++ vocabulary isn't very small... what is an epsilon?
Epslilion meaning a really small value; the idea is that floating point numbers are not stored to perfect accuracy in memory, so you will have to compare two floats to see if they are within 0.001 or whatever value you consider the best for your application.

Did a quick Google search:
http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm
thanx! I'll give it a try and let you know!
Topic archived. No new replies allowed.