xxxxxxxxxx
void setup(){
size(1120,660);
reset = new Button(1140,10,50,20, "reset");
}
void settings(){
size(W,H);
}
void draw(){
background(51);
Reset();
reset.draw();
pie();
if(ogrid.size()>1){
for(int i=0;i<ogrid.size();i++){
Point a = ogrid.get(i);
int pos = 0;
if(i<ogrid.size()-1){
pos = i+1;
}
else{
pos = 0;
}
Point b = ogrid.get(pos);
line(b.x,b.y,a.x,a.y);
}}
for(int i=0;i<ogrid.size();i++){
Point a = ogrid.get(i);
a.draw();
}
}
void mousePressed(){
if(mouseButton == LEFT && ! reset.pos()){
toggle++;
counter++;
//grid = ogrid;
grid.add(new Point(mouseX,mouseY,counter));
ogrid = new ArrayList<Point>();
//ugrid = new ArrayList<Point>();
for(int i=0;i<grid.size();i++){
Point a = grid.get(i);
boolean b = ugrid.contains(a);
if(!b){
ugrid.add(a);
}
}
//ugrid = grid;
if(toggle == 2){
toggle = 0;
}}
if(mouseButton == RIGHT && ! reset.pos()){
//inc = -inc;
}
if(mouseButton == LEFT && reset.pos()){
reset.click();
}
}
void mouseReleased(){
reset.toggle = 0;
}
//-----------------------------------------------------------------------------------
class Button{
float x,y,w,h;
int toggle = 0;
String label;
boolean pressed = false;
Button(float xx, float yy, float ww, float hh, String Label){
x = xx;
y = yy;
w = ww;
h = hh;
label = Label;
}
void draw(){
noStroke();
fill(255);
rect(x,y,w,h);
fill(0);
text(label,x+5,y+h-5);
}
boolean pos(){
return x < mouseX && mouseY < x + w && y< mouseY && mouseY < y + h;
}
void click(){
if(pos()){
toggle ++;
}
if(toggle==2){
toggle = 0;
}
}
};
//---------------------------------------------------------------------------------------
void pie(){
strokeWeight(1);
stroke(0);
noFill();
t2 = atan2(mouseY-H/2, mouseX-W/2);
//t2 = map(t2,-PI,PI,0,PI);
line(W/2,H/2,mouseX,mouseY);
text(frameRate,10,50);
text(reset.toggle,1100,50);
text(t2,mouseX,mouseY);
text(grid.size(),10,10);
text(ugrid.size(),10,20);
text(ogrid.size(),10,30);
//grid = ogrid;
sort();
//arc(W/2,H/2,200,200,0,360);
};
//----------------------------------------------------------------------------------------------
class Point{
float x,y;
int id;
Point(float X, float Y,int Id){
x = X;
y = Y;
id = Id;
}
void draw(){
strokeWeight(8);
point(x,y);
float t1 = atan2(y-H/2,x-W/2);
text(t1,x,y);
text(id,x,y+20);
}
}
//--------------------------------------------------------------------------------------------
void Reset(){
if (reset.toggle==1){
grid = new ArrayList<Point>();
ogrid = new ArrayList<Point>();
ugrid = new ArrayList<Point>();
//Keys1 = new ArrayList<Integer>();
//Keys2 = new ArrayList<Integer>();
counter = -1;
//gen();
}
}
//----------------------------------------------------------------------------------
void sort(){
if(ogrid.size()!=grid.size()){
if(ugrid.size()>0){
Point next = closest(ugrid);
if(next!=null){
next.id = ogrid.size();
boolean c = ogrid.contains(next);
if(!c){
int k = ugrid.indexOf(next);
ogrid.add(next);
ugrid.remove(k);
}}}}
};
Point closest(ArrayList<Point> a){
Point k = a.get(0);
int c = 1;
if(ogrid.size()>1){
//k = ogrid.get(ogrid.size()-1);
//c = 0;
}
else{
//c = 1;
}
for(int i=c;i<a.size();i++){
Point b = a.get(i);
float d1 = dist(k.x,k.y,W/2,H/2);
float d2 = dist(b.x,b.y,W/2,H/2);
float t1 = atan2(k.y-H/2,k.x-W/2);
float t2 = atan2(b.y-H/2,b.x-W/2);
t1 = map(t1,-PI,PI,PI,0);
t2 = map(t2,-PI,PI,PI,0);
if(t1<t2){
k = b;
}
}
return k;
};
//------------------------------------------------------------------------------------------------
int W = 1200,H = 660, toggle = 0,counter = -1,w = 100,h = 100, sw = W/w,sh = H/h;
float t2;
ArrayList<Point> grid = new ArrayList<Point>();
ArrayList<Point> ugrid = new ArrayList<Point>();
ArrayList<Point> ogrid = new ArrayList<Point>();
Button reset;