Skip to content

Instantly share code, notes, and snippets.

@KrabCode
Created September 9, 2017 12:49
Show Gist options
  • Save KrabCode/000c0c604b3ba669bae13c3e63c29456 to your computer and use it in GitHub Desktop.
Save KrabCode/000c0c604b3ba669bae13c3e63c29456 to your computer and use it in GitHub Desktop.
import processing.core.PApplet;
import java.util.ArrayList;
//import com.hamoid.VideoExport;
public class MainApp extends PApplet{
public static void main(String[] args)
{
PApplet.main("MainApp", args);
}
public void settings()
{
fullScreen();
}
//center rectangle
Point cA, cB, cC, cD;
float cSize;
//lines from the rectangle to the corners of the screen
Line tl, tr, br, bl;
ArrayList<Distance> N;
float speed;
float freq;
float acceleration;
public void setup(){
frameRate(60);
speed = 0;
freq = 30;
acceleration = 0.05f;
//setup center rectangle
cSize = 20;
cA =new Point(width/2 - cSize, height/2 - cSize);
cB =new Point(width/2 + cSize, height/2 - cSize);
cC =new Point(width/2 + cSize, height/2 + cSize);
cD =new Point(width/2 - cSize, height/2 + cSize);
//top left, right, bottom right, left
tl = new Line(cA.x, cA.y, 0, 0);
tr = new Line(cB.x, cB.y, width, 0);
br = new Line(cC.x, cC.y, width, height);
bl = new Line(cD.x, cD.y, 0, height);
N = new ArrayList<Distance>(){};
N.add(new Distance(0f, speed, acceleration));
}
public void draw(){
// draw background + trail effect
fill(255, 20);
rect(0,0,width, height);
// draw center rectangle
noStroke();
fill(0);
rect(cA.x, cA.y, cC.x- cA.x , cC.y - cA.y);
// draw lines connecting corners
//
// tl.draw();
// tr.draw();
// br.draw();
// bl.draw();
strokeWeight(4);
//draw lines connecting lines connecting corners
for(int i = 0; i < N.size(); i++){
Line left = new Line(
bl.getPointOnLineAtDistanceFromA(N.get(i).dist),
tl.getPointOnLineAtDistanceFromA(N.get(i).dist)
);
Line right = new Line(
tr.getPointOnLineAtDistanceFromA(N.get(i).dist),
br.getPointOnLineAtDistanceFromA(N.get(i).dist)
);
stroke(70, 173, 212);
left.draw();
right.draw();
Distance n = N.get(i); //n[i]+=speed;
n.vel += n.acc;
n.dist+= n.vel;
}
if(frameCount%freq==0){
N.add(new Distance(0, speed, acceleration));
cleanupOutboundLines();
}
// ex.saveFrame();
println(N.size() + " : " + frameRate);
}
void cleanupOutboundLines(){
ArrayList<Distance> toRemove = new ArrayList<>();
for(Distance n : N)
{
if(n.dist > width && n.dist > height){
toRemove.add(n);
}
}
N.removeAll(toRemove);
}
class Point{
float x, y;
public Point(float x, float y) {
this.x = x;
this.y = y;
}
}
class Distance{
float dist, vel, acc;
public Distance(float dist, float vel, float acc) {
this.dist = dist;
this.vel = vel;
this.acc = acc;
}
}
class Line{
Point A, B;
public Line(float Ax, float Ay, float Bx, float By){
A = new Point(Ax, Ay);
B = new Point(Bx, By);
}
public Line(Point A, Point B){
this.A = A;
this.B = B;
}
public Point getPointOnLineAtDistanceFromA(float dist){
float angle = getAngle(A, B);
return findPointOnEdgeOfCircle(A, dist, angle);
}
public void draw(){
line(A.x, A.y, B.x, B.y);
}
}
////////////////////////////////////////////////////////////////////////////////////////////
// LOW LEVEL MATH
////////////////////////////////////////////////////////////////////////////////////////////
/**
* Finds a point in a given angle and distance from a center point.
* @param center center point
* @param radius given distance
* @param angle given angle
* @return
*/
public Point findPointOnEdgeOfCircle(Point center, float radius, float angle)
{
return new Point(
center.x + radius * cos(angle * PI / 180),
center.y + radius * sin(angle * PI / 180)
);
}
/**
* Returns angle of a line vs the horizont
*
* @param origin start of the line
* @param end end of the line 🚆
* @return horizontal line returns 0, vertical line returns -90
*/
public float getAngle(Point origin, Point end)
{
return degrees(atan2(end.y - origin.y, end.x - origin.x));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment