Skip to content

Instantly share code, notes, and snippets.

@perilstar
Last active September 1, 2017 18:27
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 perilstar/ea2d0b4606a0d31d70285b5010353e63 to your computer and use it in GitHub Desktop.
Save perilstar/ea2d0b4606a0d31d70285b5010353e63 to your computer and use it in GitHub Desktop.
art stuff
import java.util.*;
// CONFIGURATIONS:
//Boolean wipeScreen = false;
//int[] config = new int[] {10, 180, 100, 60, 0, 14};
//void setStyle(float d) {stroke((d + c) % 360, 255, 255, 3);}
//float moveBy(float s) {return s;}
//Boolean wipeScreen = true;
//int[] config = new int[] {10, 180, 100, 60, 10, 14};
//void setStyle(float d) {stroke(d * 2, 255, 255, 20);}
//float moveBy(float s) {return s;}
//Boolean wipeScreen = false;
//int[] config = new int[] {55, 255, 100, 60, 4, 2};
//void setStyle(float d) {stroke(0, 0, (d-55));}
//float moveBy(float s) {return s-1;}
//Boolean wipeScreen = false;
//int[] config = new int[] {55, 255, 100, 120, 2, 1};
//void setStyle(float d) {stroke(0, 0, (d-55));}
//float moveBy(float s) {return 0.25*s;}
Boolean wipeScreen = false;
int[] config = new int[] {100, 200, 500, 120, 0, 2};
void setStyle(float d) {stroke(0, 0, (d-100));}
float moveBy(float s) {return .5 * s;}
//Boolean wipeScreen = true;
//int[] config = new int[] {10, 180, 200, 120, 2, 3};
//void setStyle(float d) {stroke(d*2, 255, 255- (105 + (d*2-240)));}
//float moveBy(float s) {return .5 * s;}
int minDist = config[0];
int maxDist = config[1];
int maxPointsR = config[2];
int framerate = config[3];
int strokeWeight = config[4];
int connectAmount = config[5] + 1;
List<Point> pR = new ArrayList<Point>();
Random r = new Random();
int frame = 0;
int c = 0;
List<Character> keys = new ArrayList<Character>();
boolean freeze = false;
void keyPressed() {
switch (key) {
case ' ':
pR.clear();
break;
case 'c':
pR.clear();
background(0, 0, 204);
break;
case 'f':
freeze = true;
break;
}
}
void keyReleased() {
switch (key) {
case 'f':
freeze = false;
break;
}
System.out.println(key);
}
void setup() {
colorMode(HSB, 360, 255, 255, 255);
noFill();
size(1366, 768);
frameRate(framerate);
strokeWeight(strokeWeight);
}
void draw() {
if (!freeze) {
if (wipeScreen)
clear();
if (mousePressed) {
for (int i = 0; i < r.nextInt(2) + 1; i++) {
Point p = new Point();
p.setDirection(r.nextInt(360));
p.move(15);
pR.add(p);
p.speed = (float) Math.random() * 2 + 1;
}
}
for (Point p : pR) {
p.move(moveBy(p.speed));//p.speed);
}
if (pR.size() > maxPointsR) {
pR.remove(0);
}
drawLines();
frame++;
}
}
void drawLines() {
for (int i = 0; i < pR.size(); i++) {
for (int j = Math.max(0, i); j < Math.min(pR.size(), i+connectAmount); j++) {
float dist = pR.get(i).distanceTo(pR.get(j));
//println(dist);
if ((dist < maxDist && dist > minDist) && i != j) {
setStyle(dist);
pline(pR.get(i), pR.get(j));
}
}
}
if (c < 360) {
c++;
} else {
c = 0;
}
}
void pline(List<Point> points) {
noFill();
if (points.size() == 2) {
line((float) points.get(0).getX(), (float) points.get(0).getY(), (float) points.get(1).getX(), (float) points.get(1).getY());
} else {
beginShape();
for (Point p : points) {
vertex(p.getX(), p.getY());
}
endShape();
}
}
void pline(Point... points) {
noFill();
if (points.length == 2) {
line(points[0].getX(), points[0].getY(), points[1].getX(), points[1].getY());
} else {
beginShape();
for (Point p : points) {
vertex(p.getX(), p.getY());
}
endShape();
}
}
class Point {
private float x, y;
private float direction;
public float speed;
Point() {
this.x = mouseX;
this.y = mouseY;
this.direction = 0;
}
Point(float x, float y) {
this.x = x;
this.y = y;
this.direction = 0;
}
Point(float[] arr) {
if (arr.length < 2) {
this.x = 0;
this.y = 0;
this.direction = 0;
} else {
this.x = arr[0];
this.y = arr[1];
if (arr.length == 3) {
this.direction = (float) arr[2];
} else {
this.direction = 0;
}
}
}
Point(Point source) {
this.x = source.getX();
this.y = source.getY();
this.direction = source.getDirection();
}
public Point clone() {
return new Point(this);
}
public float getX() {
return this.x;
}
public float getY() {
return this.y;
}
public float getDirection() {
return this.direction;
}
public void setX(float x) {
this.x = x;
}
public void setY(float y) {
this.y = y;
}
public Point setDirection(float dir) {
this.direction = (float) dir % 360;
return this;
}
public float changeX(float dx) {
this.x += dx;
return this.x;
}
public float changeY(float dy) {
this.y += dy;
return this.y;
}
public float[] getCoords() {
return new float[] {this.x, this.y};
}
public float distanceTo(Point p) {
return (float) (Math.sqrt(Math.pow(p.getX() - this.x, 2) + Math.pow(p.getY() - this.y, 2)));
}
public Point moveBy(float dx, float dy) {
this.x += dx;
this.y += dy;
return this;
}
public Point move(float distance) {
float dx = (float) (distance * (float) Math.cos(Math.toRadians((float) this.direction)));
//prfloatln("DIRECTION: " + this.direction);
float dy = (float) (distance * (float) Math.sin(Math.toRadians((float) this.direction)));
return this.moveBy(dx, dy);
}
public Point pointTowards(Point p) {
float dx = p.getX() - this.x;
float dy = p.getY() - this.y;
//float dir = Math.toDegrees(Math.atan2(p.getX() - this.x,p.getY() - this.y));
float dir = (float) Math.toDegrees(dy/dx);
return setDirection(dir);
}
public Point flipDirection() {
return setDirection(this.direction + 180);
}
public Point pointAwayFrom(Point p) {
pointTowards(p);
return flipDirection();
}
public Point pointTowards(String s) {
if (s.equals("mouse")) {
return pointTowards(new Point(mouseX, mouseY));
}
if (s.equals("center")) {
return pointTowards(new Point(width / 2, height / 2));
}
return this;
}
public Point pointAwayFrom(String s) {
if (s.equals("mouse")) {
return pointAwayFrom(new Point(mouseX, mouseY));
}
if (s.equals("center")) {
return pointAwayFrom(new Point(width / 2, height / 2));
}
return this;
}
public boolean equals(Point p) {
return p.getX() == this.x && p.getY() == this.y && p.getDirection() == this.direction;
}
}
@perilstar
Copy link
Author

I'm aware it's poorly optimised.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment