“points on grid” by paul goux
https://openprocessing.org/sketch/783497
License CreativeCommons Attribution ShareAlike
https://creativecommons.org/licenses/by-sa/3.0
{{filePath}}
{{width}} x {{height}}
Report Sketch
Oh, that naughty sketch! Please let us know what the issue is below.
Apply Template
Applying this template will reset your sketch and remove all your changes. Are you sure you would like to continue?
Report Sketch
Report Comment
Please confirm that you would like to report the comment below.
We will review your submission and take any actions necessary per our Community Guidelines. In addition to reporting this comment, you can also block the user to prevent any future interactions.
Please report comments only when necessary. Unnecessary or abusive use of this tool may result in your own account being suspended.
Are you sure you want to delete your sketch?
Any files uploaded will be deleted as well.
Delete Comment?
This will also delete all the replies to this comment.
Delete this tab? Any code in it will be deleted as well.
Select a collection to submit your sketch
We Need Your Support
Since 2008, OpenProcessing has provided tools for creative coders to learn, create, and share over a million open source projects in a friendly environment.
Niche websites like ours need your continued support for future development and maintenance, while keeping it an ad-free platform that respects your data and privacy!
Please consider subscribing below to show your support with a "Plus" badge on your profile and get access to many other features!
A fork of points around a circle by paul goux
CC Attribution ShareAlike
points on grid
goux
xxxxxxxxxx
void setup(){
size(1200,660);
reset = new Button(1000,30,50,20, "reset");
};
boolean moused;
void draw(){
background(51);
stroke(255);
for(int i=0;i<sh;i++){
strokeWeight(1);
line(0 + sw*i, 0, 0 + sw*i, H);
}
for(int j=0;j<sw;j++){
strokeWeight(1);
line(0, 0 + sh*j, W, 0 + sh*j);
}
Reset();
reset.draw();
pie();
float t1 = 0;
float t2 = 0;
float c1 = 0;
if(ogrid.size()>1){
for(int i=0;i<ogrid.size();i++){
Point a = ogrid.get(i);
int pos = 0;
Point b = null;
if(i<ogrid.size()-1)b = ogrid.get(i+1);
else b = ogrid.get(0);;
//t1 += abs((atan2(mouseY - b.y,mouseX - b.x))-(atan2(mouseY - a.y,mouseX - a.x)));
t1 = abs(atan2(mouseY - a.y,mouseX - a.x)+PI);
t2 = abs(atan2(mouseY - b.y,mouseX - b.x)+PI);
stroke(0);
if(abs(t2-t1)>=PI){
c1++;
}
// text(degrees(t3),a.x+50,a.y+40);
}
if(mousePressed&&mouseButton==RIGHT)println();
stroke(0);
beginShape();
//noStroke();
for(int i=0;i<ogrid.size();i++){
Point a = ogrid.get(i);
Point b = null;
if(i<ogrid.size()-1)b = ogrid.get(i+1);
else b = ogrid.get(0);
if(c1%2!=0){
fill(255);
// ellipse(mouseX,mouseY,20,20);
}
vertex(a.x,a.y);
vertex(b.x,b.y);
line(b.x,b.y,a.x,a.y);
// text(i,a.x,a.y);
}
endShape(CLOSE);
stroke(0);
for(int i=0;i<ogrid.size();i++){
Point a = ogrid.get(i);
Point b = null;
if(i<ogrid.size()-1)b = ogrid.get(i+1);
else b = ogrid.get(0);
// line(b.x,b.y,a.x,a.y);
// text(i,a.x,a.y);
}
}
strokeWeight(20);
point(midpoint.x,midpoint.y);
for(int i=0;i<ogrid.size();i++){
Point a = ogrid.get(i);
a.draw();
}
fill(255,0,0);
// text(c1,mouseX+30,mouseY);
}
void mousePressed(){
if(mouseButton == LEFT && ! reset.pos()&&!moused){
toggle++;
moused = true;
counter++;
//grid = ogrid;
grid.add(new Point(floor(mouseX/sw)*sw,floor(mouseY/sh)*sh,counter));
midpoint = new PVector(0,0);
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);
midpoint.x += a.x;
midpoint.y += a.y;
}
}
midpoint.x = midpoint.x / grid.size()-1;
midpoint.y = midpoint.y / grid.size()-1;
//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;
moused = false;
}
//------------------------------------------------------------
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 = atan2(mouseY-midpoint.y, mouseX-midpoint.x);
//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);
// float t1 = atan2(y-midpoint.y,x-midpoint.x);
// text(t1,x+20,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;
midpoint = new PVector(0,0);
//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);
float d1 = dist(k.x,k.y,midpoint.x,midpoint.y);
float d2 = dist(b.x,b.y,midpoint.x,midpoint.y);
float t1 = atan2(k.y-midpoint.y,k.x-midpoint.x);
float t2 = atan2(b.y-midpoint.y,b.x-midpoint.x);
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,counted,w = 20,h = 20, sw = W/w, sh = H/h;
float t2;
PVector midpoint = new PVector(0,0);
ArrayList<Point> grid = new ArrayList<Point>();
ArrayList<Point> ugrid = new ArrayList<Point>();
ArrayList<Point> ogrid = new ArrayList<Point>();
Button reset;
See More Shortcuts