Skip to content

Instantly share code, notes, and snippets.

@clankill3r
Created January 11, 2019 00:00
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 clankill3r/6f2e212f4eb71deaa38e608f9b4705a9 to your computer and use it in GitHub Desktop.
Save clankill3r/6f2e212f4eb71deaa38e608f9b4705a9 to your computer and use it in GitHub Desktop.
colored line
void setup() {
size(600, 600);
}
void draw() {
background(255);
PVector a = new PVector(mouseX, mouseY);
PVector b = new PVector(50, height/2);
float w_a = 50;
float w_b = 150;
int color_a = color(255,0,0);
int color_b = color(0,0,255);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
PVector v = point_nearest_line(a, b, new PVector(x, y));
if (v == null) continue;
float t = dist(v.x, v.y, a.x, a.y) / dist(a.x, a.y, b.x, b.y);
t = constrain(t, 0, 1);
float w = map(t, 0, 1, w_b, w_a);
float dist_to_line = dist(v.x, v.y, x, y);
if (dist_to_line <= w) {
float t2 = dist_to_line / w;
int c = lerpColor(color_a, color_b, t2);
set(x, y, c);
}
}
}
}
public float NEARNESS = 1.0;
public PVector point_nearest_line(PVector v0, PVector v1, PVector p){
PVector vp = null;
PVector the_line = PVector.sub(v1, v0);
float lineMag = the_line.mag();
lineMag = lineMag * lineMag;
if (lineMag > 0.0) {
PVector pv0_line = PVector.sub(p, v0);
float t = pv0_line.dot(the_line)/lineMag;
if (t >= 0 && t <= 1){
vp = new PVector();
vp.x = the_line.x * t + v0.x;
vp.y = the_line.y * t + v0.y;
}
}
return vp;
}
@clankill3r
Copy link
Author

screen shot 2019-01-11 at 01 03 13

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