import [Link].
*;
import [Link];
Serial myPort;
String angle = "";
String distance = "";
String data = "";
int iAngle = 0, iDistance = 0;
ArrayList<PVector> points;
PFont myFont;
boolean resetPoints = false;
void setup() {
size(800, 600);
smooth();
myPort = new Serial(this, "COM5", 9600);
[Link]('.');
points = new ArrayList<PVector>();
myFont = createFont("Consolas", 16);
textFont(myFont);
}
void draw() {
background(0, 100, 0); // xanh đậm
drawRadar();
drawObjects();
drawSweep();
drawText();
// Nếu vừa reset
if (resetPoints) {
[Link]();
resetPoints = false;
}
}
void serialEvent(Serial myPort) {
data = [Link]('.');
if (data != null) {
data = [Link](0, [Link]() - 1);
int index = [Link](',');
if (index > 0) {
angle = [Link](0, index);
distance = [Link](index + 1);
iAngle = int(angle);
iDistance = int(distance);
// Khi quét về 0 độ => xoá điểm
if (iAngle == 15 || iAngle == 165) {
resetPoints = true;
}
if (iDistance < 40) {
float r = map(iDistance, 0, 40, 0, width/2 * 0.9);
float realAngle = radians(iAngle);
float x = r * cos(realAngle);
float y = -r * sin(realAngle);
[Link](new PVector(x, y));
}
}
}
}
void drawRadar() {
pushMatrix();
translate(width/2, height*0.95);
stroke(0, 255, 0);
strokeWeight(2);
noFill();
for (int r = 1; r <= 4; r++) {
ellipse(0, 0, r * width/2 * 0.45, r * width/2 * 0.45);
}
for (int a = 0; a <= 180; a += 30) {
float x = (width/2 * 0.9) * cos(radians(a));
float y = -(width/2 * 0.9) * sin(radians(a));
line(0, 0, x, y);
}
popMatrix();
}
void drawObjects() {
pushMatrix();
translate(width/2, height*0.95);
noStroke();
fill(255, 153, 0); // cam
for (int i = 0; i < [Link](); i++) {
ellipse([Link](i).x, [Link](i).y, 6, 6);
}
popMatrix();
}
void drawSweep() {
pushMatrix();
translate(width/2, height*0.95);
stroke(255);
strokeWeight(2);
float x = (width/2 * 0.9) * cos(radians(iAngle));
float y = -(width/2 * 0.9) * sin(radians(iAngle));
line(0, 0, x, y);
popMatrix();
}
void drawText() {
fill(0, 200);
noStroke();
rect(0, 0, width, 40);
fill(255);
textAlign(LEFT);
text("Angle: " + iAngle + "°", 10, 25);
text("Distance: " + iDistance + " cm", 200, 25);
}