721
edits
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){ | ||
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; | ||
}</ | }</syntaxhighlight> | ||
Note that, for the sqrt() function to be known to the compiler, you need to include the math library with < | Note that, for the sqrt() function to be known to the compiler, you need to include the math library with <syntaxhighlight lang="c++" >#include <math.h></syntaxhighlight> | ||
[https://replit.com/@abnutzer/Homework-1-Distance-Function Distance Function Complete Code] | [https://replit.com/@abnutzer/Homework-1-Distance-Function Distance Function Complete Code] | ||
Line 52: | Line 52: | ||
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: < | * sum up our points: <syntaxhighlight lang="c++">p0 + p1 + p2 + p3 ... = sum </code> | ||
* divide the sum by out number of point: | * divide the sum by out number of point: | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="c++"> | ||
centroid = sum / num_of_points | centroid = sum / num_of_points | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 63: | Line 63: | ||
Note: We sum up points simply by adding up their x and y values. E.g. | Note: We sum up points simply by adding up their x and y values. E.g. | ||
< | <syntaxhighlight lang="c++"> | ||
p0 = (1,1), p1= (4,2) | p0 = (1,1), p1= (4,2) | ||
c = p0 + p1; | c = p0 + p1; | ||
c = (1,1) + (4,2) = (5,3) | c = (1,1) + (4,2) = (5,3) | ||
</ | </syntaxhighlight> | ||
Dividing the point by a number, means to divide both dimensions by the same number: | Dividing the point by a number, means to divide both dimensions by the same number: | ||
< | <syntaxhighlight lang="c++"> | ||
centroid = p2 /3 = (5,3) / 2 = (2.5,1.5) | centroid = p2 /3 = (5,3) / 2 = (2.5,1.5) | ||
</ | </syntaxhighlight> | ||
In this case our centroid is (2.5,1.5). As you can see from the drawing above, the centroid lies exactly in the middle of our two points. | In this case our centroid is (2.5,1.5). As you can see from the drawing above, the centroid lies exactly in the middle of our two points. | ||
Line 79: | Line 79: | ||
Extend our Point2D class to have the following functions: | Extend our Point2D class to have the following functions: | ||
< | <syntaxhighlight lang="c++"> | ||
Point2D operator+(Point2D& p2); | Point2D operator+(Point2D& p2); | ||
</ | </syntaxhighlight> | ||
This function should add up two points and return a new Point2D, which is the sum of the two points. | This function should add up two points and return a new Point2D, which is the sum of the two points. | ||
If you implement this function you can then simply write: Point2D sum = p0 + p1; (where p0 and p1 are of the class Point2D) | If you implement this function you can then simply write: Point2D sum = p0 + p1; (where p0 and p1 are of the class Point2D) | ||
< | <syntaxhighlight lang="c++"> | ||
Point2D operator/(float f); | Point2D operator/(float f); | ||
</ | </syntaxhighlight> | ||
This function should divide a point by the number f and return a new Point2D which is the divided point. | This function should divide a point by the number f and return a new Point2D which is the divided point. | ||
If you implement this function you can then simply write: | If you implement this function you can then simply write: | ||
< | <syntaxhighlight lang="c++"> | ||
Point2D div = p0 / 5; | Point2D div = p0 / 5; | ||
</ | </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!) | ||
Line 98: | Line 98: | ||
Here are the points to calculate the centroid for: | Here are the points to calculate the centroid for: | ||
< | <syntaxhighlight lang="c++"> | ||
Point2D p0(0,0); | Point2D p0(0,0); | ||
Point2D p1(0,1); | Point2D p1(0,1); | ||
Point2D p2(1,0); | Point2D p2(1,0); | ||
Point2D p3(1,1); | Point2D p3(1,1); | ||
</ | </syntaxhighlight> | ||
And here is a code template to start coding from: https://repl.it/@abnutzer/Homework-2 | And here is a code template to start coding from: https://repl.it/@abnutzer/Homework-2 | ||
Press the "fork" button to work on your own copy of the code! | Press the "fork" button to work on your own copy of the code! | ||
Later in the class, our points will not be two dimensional anymore, but contain different features of our sounds. E.g. it could be 3D: A dimension for the loudness of the bass, one for the mids and one for the treble. In general the resolution of the spectral data will be much higher and thus a point of 128 dimensions or more can be considered. | Later in the class, our points will not be two dimensional anymore, but contain different features of our sounds. E.g. it could be 3D: A dimension for the loudness of the bass, one for the mids and one for the treble. In general the resolution of the spectral data will be much higher and thus a point of 128 dimensions or more can be considered. |