Skip to content

Instantly share code, notes, and snippets.

@neosarchizo
Last active May 14, 2016 07:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neosarchizo/4f7c492d22d3db2f4855c100250690b3 to your computer and use it in GitHub Desktop.
Save neosarchizo/4f7c492d22d3db2f4855c100250690b3 to your computer and use it in GitHub Desktop.
[아두이노, 상상을 현실로 만드는 프로젝트 심화편] 코드 7 - 2
class Ball {
int life = 50;
float x, y;
Ball(float cx, float cy, int degree, int distance) {
float d = map(distance, 0, 100, 0, radius);
float rad = TWO_PI-map(degree, 0, 360, 0, TWO_PI);
x = cx + cos(rad)*d;
y = cy + sin(rad)*d;
}
void display() {
ellipse(x, y, life, life);
}
void update() {
--life;
if (life < 0 )
life = 0;
}
boolean isDead() {
return life == 0;
}
}
import processing.serial.*;
Serial myPort;
int degree = 0, radius = 200;
float cx, cy;
ArrayList<Ball> balls = new ArrayList<Ball>();
void setup()
{
size(400, 200);
cx = width/2;
cy = height;
noFill();
stroke(0, 255, 36);
myPort = new Serial(this, "Your Arduino Port", 9600);
}
void draw() {
background(0);
ellipse(cx, cy, 2 * radius, 2 * radius);
float rad = TWO_PI-map(degree, 0, 360, 0, TWO_PI);
line(cx, cy, cx + cos(rad)* radius, cy + sin(rad)* radius);
updateBalls();
displayBalls();
}
void updateBalls() {
for (int i = balls.size()-1; i > -1; i--) {
balls.get(i).update();
if (balls.get(i).isDead())
balls.remove(i);
}
}
void displayBalls() {
for (int i = 0; i < balls.size() - 1; i++) {
balls.get(i).display();
}
}
void serialEvent(Serial p) {
String inString = p.readStringUntil('\n');
if (inString != null) {
if (inString.startsWith("r")) {
String[] strings = inString.trim().replace("r", "").split("d");
if (strings.length > 1) {
degree = Integer.parseInt(strings[0]);
int distance = Integer.parseInt(strings[1]);
if (distance != 0) {
balls.add(new Ball(cx, cy, degree, distance));
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment