Game Programming - First Steps (Point Structure)

I'm starting to move into game programming using Allegro 5 as my graphics library. I'd like feedback on the point structure below. This is the base structure that I'll build everything on, to include rectangles, grids, graphics, textbox and button positioning, and coordinate tracking on the backbuffer and window.

Edit: Specifically, are the functions below sufficient? Will the structure itself cause any errors down the road in the way the X and Y values are handled? I realize they are not encapsulated, since they are public, but I don't want to restrict the values beyond the normal scope of a 'float' variable type.

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

#ifndef POINT_H
#define POINT_H

#include <iostream>
#include <math.h>

struct point {
	float X, Y;
	point(float X, float Y) :X(X),Y(Y) {};
	point(float N) :X(N),Y(N) {};
	point() :X(0.0),Y(0.0) {};

	float Slope(point p)
	{
		//Slope = m
		// m = (y2-y1) / (x2-x1)
		float m = ((p.Y-Y) / (p.X-X));
		return m;
	}

	float Distance(point p)
	{
		//Distance = d
		//d = square_root of [ (x2-x1)^2 + (y2-y1)^2 ]
		float d = ((p.X-X) * (p.X-X)) + ((p.Y-Y) * (p.Y-Y));
		d = sqrt(d);
		return d;
	}

	point Increment(point p, float dist)
	{
		//Find a point between this point and point(p), adding a specified distance(dist)
		//New x position = Nx
		//New y position = Ny
		//Nx = X + (dist / square root of [ (m^2 + 1) ])
		//Ny = Y + (m*dist / square root of [ (m^2 + 1) ])
		float m = Slope(p);

		float Nx = dist / (sqrt((m*m) + 1));
		if (m >= 0.0) Nx = ((X < p.X) ? (X+Nx):(p.X+Nx));
		if (m < 0.0) Nx = ((X < p.X) ? (X+Nx):(p.X+Nx));

		float Ny = (m * dist) / (sqrt((m*m) + 1));
		if (m >= 0.0) Ny = ((Y < p.Y) ? (Y+Ny):(p.Y+Ny));
		if (m < 0.0) Ny = ((Y < p.Y) ? (p.Y+Ny):(Y+Ny));

		return point(Nx,Ny);
	}

	float Magnitude()
	{
		//Distance from 0,0 (if point is a vector)
		return Distance(point(0,0));
	}

	point Normalize()
	{
		float nX = (X/Magnitude());
		float nY = (Y/Magnitude());
		return point(nX,nY);
	}
	
	//Addition
	point operator+=(point pnt){ (*this).X += pnt.X; (*this).Y += pnt.Y; return (*this); }
	point operator+=(float num){ (*this).X += num; (*this).Y += num; return (*this); }
	point operator+(point pnt) { return point((*this).X + pnt.X, (*this).Y + pnt.Y);	}
	point operator+(float num) { return point((*this).X + num, (*this).Y + num); }

	//Subtraction
	point operator-=(point pnt){ (*this).X -= pnt.X; (*this).Y -= pnt.Y; return (*this); }
	point operator-=(float num){ (*this).X -= num; (*this).Y -= num; return (*this); }
	point operator-(point pnt) { return point((*this).X - pnt.X, (*this).Y - pnt.Y); }
	point operator-(float num) { return point((*this).X - num, (*this).Y - num); }

	//Multiplication
	point operator*=(point pnt){ (*this).X *= pnt.X; (*this).Y *= pnt.Y; return (*this); }
	point operator*=(float num){ (*this).X *= num; (*this).Y *= num; return (*this); }
	point operator*(point pnt) { return point((*this).X * pnt.X, (*this).Y * pnt.Y); }
	point operator*(float num) { return point((*this).X * num, (*this).Y * num); }

	//Division
	point operator/=(point pnt){ (*this).X /= pnt.X; (*this).Y /= pnt.Y; return (*this); }
	point operator/=(float num){ (*this).X /= num; (*this).Y /= num; return (*this); }
	point operator/(point pnt) { return point((*this).X / pnt.X, (*this).Y / pnt.Y); }
	point operator/(float num) { return point((*this).X / num, (*this).Y / num); }

	//Equal (Assignment)
	point operator=(point pnt) { (*this).X = pnt.X; (*this).Y = pnt.Y; return (*this); }
	point operator=(float num) { (*this).X = num; (*this).Y = num; return (*this); }
};

	bool operator==(point a, point b) { return a.X==b.X && a.Y==b.Y; }
	bool operator==(point a, float num) { return a.X==num && a.Y==num; }

	bool operator!=(point a, point b) { return !(a==b); }
	bool operator!=(point a, float num) { return !(a.X==num && a.Y==num); }

	std::ostream& operator<<(std::ostream& os, const point a)
	{
	os << "(" << a.X << "," << a.Y << ")";
	return os;
	}

#endif
Last edited on
Maybe you want to add a few overloads so that the order of some of the operators doesn't matter.

Right now you can do point * num but you can't do num * point
Thanks. I added the following before the #endif statement...

1
2
3
//Other operator overloads
point operator+(float num, point pnt) { return (pnt + num); }
point operator*(float num, point pnt) { return (pnt * num); }


I don't think the subtraction or division make sense in this case. For example, 1 - (2,2) , I wouldn't even know what this would become? (1,1) if it was the other way around based on the way I programmed the point structure. Same with division.
One more question.

I don't have a destructor. Would one be necessary for this structure? Should I delete the memory used by the X and Y float data types?
No. You only need to use delete when you have created something with new.
Topic archived. No new replies allowed.