C++ Unsure of minor implementation

Hi, I am in need of urgent help. I am unsure of how to implement the computation of the average of my vector points of x and y in my polygon's constructor. I have a polygon class, point class and a vector class. Polygon class inherits from point class. Polygon class uses base point class as a pivot point and provides the vertices(a collection of points). This is a subset of my entire code. I've made it compilable. Appreciate any of your help.

My output should be 5 and 8 instead of 5 and 4.

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

template <typename T>
class Vector
{
public:

	typ x() const;
	typ y() const;

	Vector();
	Vector(type x, type y);
 

private:
	typ _x;
	typ _y;
};

template <typename T>
	Vector<T>::Vector() :
		_x{},
		_y{}
	{
	}


	template <typename T>
	Vector<T>::Vector(type x, type y) :
		_x{x},
		_y{y}
	{
	}

	template <typename T>
	typename Vector<T>::type Vector<T>::x() const
	{
		return _x;
	}

	template <typename T>
	typename Vector<T>::type Vector<T>::y() const
	{
		return _y;
	}
 

template<typename T>
class Point 
{
public:

	Point() {}
	Point( type pots) : _points(pots) {}

	int x() const
	{
		return _points.x();
	}
	int y() const
	{
		return _points.y();
	}
 
protected:
	type _points;
 };
template<typename T>
class polygonn  :public Point<T>
{
public:
	polygonn( unsigned int size, T* verts) : Point<T>(*verts), _size(size)
	{

	}

private:
	unsigned int _size;
};
int main()
{
	using vector = Vector<int>;
	std::array<vectr, 3> pts = {
				vectr{5, 4},
				vectr{0, 7},
				vectr{10, 13}
	};
	const polygonn<vectr> polygonn{
		pts.size(),
		pts.data()
	};
	if ((polygonn.x() != 5) || (polygonn.y() != 8)) // (5 + 0 + 10)/3 || (4 + 7 + 13)/3
	{
		std::cout << polygonn.x() << std::endl;
		std::cout << polygonn.y() << std::endl;
		std::cout << "Failed construction at position." << std::endl;
	}

}
Last edited on
(5 + 0 + 10)/3
Where in your code are you doing any sort of averaging?

Also, just curious, are you in the same class as sparki (http://www.cplusplus.com/forum/beginner/271234/)?
Last edited on
Hi, I don't think I know Sparki. Haha. I need to compute the average of vector{5, 4},vector{0, 7},vector{10, 13} to get vector{5,8}. which is what I want to do, but unsure of how to implement as i'm not very well versed with programming, but trying my best.
Last edited on
Okay, so your "polygonn" object takes in the number of points and the array of points itself.

When you do Point<T>(*verts), you're doing the same thing as Point<T>(verts[0]) so you're essentially only passing the first vertex. In other words, kinda useless.

I think a "has-a" relationship would make much more sense here than an is-a relationship. A polygon has an array of points that define it, and taking the average of those points could be an additional operation. But, if the end result needs to be that polygon.x() == 5 and polygon.y() == 8, you need to compute the average as you mentioned, and eventually assign it to _pts.

I would change your polygon constructor so it assigns a new value to _pts, which is computed by taking the average of the x coordinates of verts, and the average of the y coordinates of verts.

Let's say you have a function like this:
1
2
3
4
5
6
7
Vector<int> average(Vector<int> points[], int size)
{
    int x_sum = 0;
    int y_sum = 0;
    // .... compute sum and then divide by number of points
    return Vector<int>(x_sum / size, y_sum / size);
}

To compute the average, sum the x components and then divide by the total number of values, and sum the y components then divide that by the total number of values. Try to write that.
Last edited on
Hey thanks. I got it! Appreciate it alot man:)
Topic archived. No new replies allowed.