Created
January 11, 2019 00:00
-
-
Save clankill3r/6f2e212f4eb71deaa38e608f9b4705a9 to your computer and use it in GitHub Desktop.
colored line
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
Author
clankill3r
commented
Jan 11, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment