Skip to content

Instantly share code, notes, and snippets.

@nataliefreed
Created October 7, 2014 00:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nataliefreed/a6f5faedd506c07b6f70 to your computer and use it in GitHub Desktop.
Save nataliefreed/a6f5faedd506c07b6f70 to your computer and use it in GitHub Desktop.
Virtual "graph paper" for Cartesian points. Click on the screen to add a point, click and drag on points to move them and see their coordinates change
//Click to add a new point
//Drag points to see their coordinates change
//Natalie Freed Oct. 2014
DraggableHandler points;
void setup()
{
size(600, 600);
points = new DraggableHandler(this);
points.addToEnd(new DraggableCircle(30, height/2, 10, color(0, 100, 0, 150)));
}
void draw()
{
background(255);
stroke(100, 50);
drawGrid();
points.display();
}
void drawGrid()
{
for(int i=0;i<width;i+=30)
{
line(i, 0, i, height);
}
for(int i=0;i<width;i+=30)
{
line(0, i, width, i);
}
}
class DraggableCircle implements Draggable
{
PVector center;
int radius;
boolean dragged;
int fillColor;
PVector clickOffset; //offset of cursor from center
DraggableCircle(int centerX, int centerY, int tempRadius, int tempColor)
{
center = new PVector(centerX, centerY);
radius = tempRadius;
fillColor = tempColor;
dragged = false;
PVector clickOffset = new PVector(0, 0);
}
void display()
{
noStroke();
fill(fillColor);
ellipse(center.x, center.y, radius*2, radius*2);
fill(0);
textSize(16);
text("(" + round(center.x) + "," + round(center.y) + ")", center.x, center.y);
}
float getX()
{
return center.x;
}
float getY()
{
return center.y;
}
boolean isOver(int x, int y)
{
//return true if distance between point and center is smaller than radius
if (dist(center.x, center.y, x, y) <= radius)
{
return true;
}
else
{
return false;
}
}
void update() //move with mouse
{
if (dragged)
{
if(mouseX > 0 && mouseX < width && mouseY > 0 && mouseY < height) //don't let it get away!
{
center.set(PVector.add(new PVector(mouseX, mouseY), clickOffset));
}
}
}
void setDragged(int fromX, int fromY)
{
dragged = true;
clickOffset = PVector.sub(center, new PVector(fromX, fromY));
}
void releaseDragged()
{
dragged = false;
}
}
//You don't need to change anything below here!
//Here in case you want to peek under the hood, but
//there's some slightly weird stuff.
interface Draggable
{
void update();
void display();
void setDragged(int fromX, int fromY);
void releaseDragged();
float getX();
float getY();
boolean isOver(int x, int y);
}
public class DraggableHandler
{
ArrayList<Draggable> draggables;
Draggable currentDragged;
DraggableHandler(PApplet app)
{
draggables = new ArrayList<Draggable>();
currentDragged = null;
app.registerMethod("draw", this);
app.registerMethod("mouseEvent", this);
}
void addToEnd(Draggable d)
{
draggables.add(d);
}
void addToFront(Draggable d)
{
draggables.add(0, d); //add to index 0, shift all others
}
void remove(Draggable d)
{
draggables.remove(d);
}
void display()
{
for (int i=draggables.size()-1;i>=0;i--) //draw backwards so first will be on top
{
draggables.get(i).display();
}
}
public void draw() //this draw is automatically called at the end of each draw()
{
for (int i=0;i<draggables.size();i++)
{
draggables.get(i).update();
}
}
public void mouseEvent(MouseEvent event)
{
int x = event.getX();
int y = event.getY();
switch (event.getAction()) {
case MouseEvent.PRESS:
// choose top draggable to drag if clicked on
if (currentDragged == null)
{
for (int i=0;i<draggables.size();i++)
{
if (draggables.get(i).isOver(x, y))
{
currentDragged = draggables.get(i);
currentDragged.setDragged(x, y);
break;
}
}
}
if (currentDragged == null)
{
addToEnd(new DraggableCircle(x, y, 10, color(0, 100, 0, 150)));
}
break;
case MouseEvent.RELEASE:
// release any dragged objects
if (currentDragged != null)
{
currentDragged.releaseDragged();
currentDragged = null;
}
break;
case MouseEvent.CLICK:
//
break;
case MouseEvent.DRAG:
//
break;
case MouseEvent.MOVE:
//
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment