721
edits
No edit summary |
No edit summary |
||
Line 8: | Line 8: | ||
To get the x- and y-coordinates in our code, we can use the private variables for point p1 (_x and _y) and the member functions getX() and getY() for point p2. So the most crucial lines of code are: | To get the x- and y-coordinates in our code, we can use the private variables for point p1 (_x and _y) and the member functions getX() and getY() for point p2. So the most crucial lines of code are: | ||
<syntaxhighlight lang="c++">float Point2D::calculateDistanceToOtherPoint(Point2D p2){ | <syntaxhighlight lang="c++"> | ||
float Point2D::calculateDistanceToOtherPoint(Point2D p2){ | |||
float distance = sqrt( (p2.getX() -_x)*( p2.getX() -_x)+( p2.getY()-_y)*( p2.getY() -_y) ); | float distance = sqrt( (p2.getX() -_x)*( p2.getX() -_x)+( p2.getY()-_y)*( p2.getY() -_y) ); | ||
return distance; | return distance; | ||
Line 31: | Line 32: | ||
When we create a pointer to our Point2D, the syntax is like this: | When we create a pointer to our Point2D, the syntax is like this: | ||
<syntaxhighlight lang="c++" >Point2D* p2 = new Point2D(1,2); | <syntaxhighlight lang="c++" > | ||
Point2D* p2 = new Point2D(1,2); | |||
... | ... | ||
delete p2;</syntaxhighlight> | delete p2; | ||
</syntaxhighlight> | |||
We need to use the asterisk to denote, that we want to create a pointer and we need to use the "new" keyword to reserve the memory for our Point2D. After the "new" keyword we can initialize our Point2D directly as shown before. | We need to use the asterisk to denote, that we want to create a pointer and we need to use the "new" keyword to reserve the memory for our Point2D. After the "new" keyword we can initialize our Point2D directly as shown before. | ||
Line 52: | Line 55: | ||
To calculate a centroid of a collection of points (our cluster) we need to do two steps: | To calculate a centroid of a collection of points (our cluster) we need to do two steps: | ||
* sum up our points: <syntaxhighlight lang="c++">p0 + p1 + p2 + p3 ... = sum </ | * sum up our points: | ||
<syntaxhighlight lang="c++">p0 + p1 + p2 + p3 ... = sum </syntaxhighlight> | |||
* divide the sum by out number of point: | * divide the sum by out number of point: | ||
<syntaxhighlight lang="c++"> | <syntaxhighlight lang="c++"> | ||
Line 91: | Line 95: | ||
<syntaxhighlight lang="c++"> | <syntaxhighlight lang="c++"> | ||
Point2D div = p0 / 5; | Point2D div = p0 / 5; | ||
</syntaxhighlight>(where p0 is of class Point2D) | </syntaxhighlight> | ||
(where p0 is of class Point2D) | |||
Write an algorithm that calculates the centroid of the given points | Write an algorithm that calculates the centroid of the given points | ||
use a for loop to add up any given number of points (for this, out the points into an array and the access the array in the loop!) | use a for loop to add up any given number of points (for this, out the points into an array and the access the array in the loop!) |