-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlob.java
More file actions
110 lines (94 loc) · 2.06 KB
/
Blob.java
File metadata and controls
110 lines (94 loc) · 2.06 KB
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
107
108
109
110
import java.awt.Graphics;
/**
* Animated blob, defined by a position and size,
* and the ability to step (move/grow), draw itself, and see if a point is inside.
*
* @author Chris Bailey-Kellogg, Dartmouth CS 10, Spring 2016, based on animated agents from previous terms
* @author CBK, Fall 2016, implements Point2D
*/
public class Blob implements Point2D {
protected double x, y; // position
protected double dx=0, dy=0; // velocity, defaults to none
protected double r=5; // radius
protected double dr=0; // growth step (size and sign), defaults to none
public Blob() {
// Do nothing; everything has its default value
// This constructor is implicit unless you provide an alternative
}
/**
* @param x initial x coordinate
* @param y initial y coordinate
*/
public Blob(double x, double y) {
this.x = x;
this.y = y;
}
/**
* @param x initial x coordinate
* @param y initial y coordinate
* @param r initial radius
*/
public Blob(double x, double y, double r) {
this.x = x;
this.y = y;
this.r = r;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public double getR() {
return r;
}
public void setR(double r) {
this.r = r;
}
/**
* Sets the velocity.
* @param dx new dx
* @param dy new dy
*/
public void setVelocity(double dx, double dy) {
this.dx = dx;
this.dy = dy;
}
/**
* Sets the direction of growth.
* @param dr new dr
*/
public void setGrowth(double dr) {
this.dr = dr;
}
/**
* Updates the blob, moving & growing.
*/
public void step() {
x += dx;
y += dy;
r += dr;
}
/**
* Tests whether the point is inside the blob.
* @param x2
* @param y2
* @return is (x2,y2) inside the blob?
*/
public boolean contains(double x2, double y2) {
return (x-x2)*(x-x2) + (y-y2)*(y-y2) <= r*r;
}
/**
* Draws the blob on the graphics.
* @param g
*/
public void draw(Graphics g) {
g.fillOval((int)(x-r), (int)(y-r), (int)(2*r), (int)(2*r));
}
}