-
Notifications
You must be signed in to change notification settings - Fork 0
/
Blob.java
61 lines (53 loc) · 1.84 KB
/
Blob.java
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
/*
* Blob.java
* ------------------------------------------------
* Helper class that defines a Blob of `mass` pixels.
*
* Compilation: javac-introcs Blob.java
* Execution: java-introcs Blob
*/
public class Blob{
private int mass; //number of pixels in this Blob
public double sumOfMassX; //sum of the x-positions of all pixels
public double sumOfMassY; //sum of the y-positions of all pixels
//Blob constructor
public Blob(){
mass = 0;
sumOfMassX = 0;
sumOfMassY = 0;
}
//Adds pixel at position (i, j) to this Blob
public void add(int i, int j){
mass++;
sumOfMassX = sumOfMassX + i;
sumOfMassY = sumOfMassY + j;
}
//Returns the number of points in the Blob
public int mass(){
return mass;
}
//Returns the distance from this Blob to Blob `that`
public double distanceTo(Blob b){
double centerMassX = (sumOfMassX/mass) - (b.sumOfMassX/b.mass() );
double centerMassY = (sumOfMassY/mass) - (b.sumOfMassY/b.mass() );
centerMassX = centerMassX * centerMassX;
centerMassY = centerMassY * centerMassY;
return Math.sqrt( (centerMassX + centerMassY) );
}
//Formats relevant information from the Blob and returns it as a string
public String toString(){
return String.format("%3d (%8.4f, %8.4f)", mass,
(sumOfMassX/mass), (sumOfMassY/mass) );
}
public static void main(String[] args){
Blob b = new Blob();
Blob p = new Blob();
for( int i = 0; i < 10; i++)
{b.add(i, 1);
p. add(1, i);}
StdOut.println(b);
StdOut.println(p);
StdOut.println();
StdOut.println(b.distanceTo(p) );
}
}