“Spoon-Intersections” by Spoon
https://openprocessing.org/sketch/585329
License CreativeCommons Attribution ShareAlike
https://creativecommons.org/licenses/by-sa/3.0
{{filePath}}
{{width}} x {{height}}
Report Sketch
Oh, that naughty sketch! Please let us know what the issue is below.
Apply Template
Applying this template will reset your sketch and remove all your changes. Are you sure you would like to continue?
Report Sketch
Report Comment
Please confirm that you would like to report the comment below.
We will review your submission and take any actions necessary per our Community Guidelines. In addition to reporting this comment, you can also block the user to prevent any future interactions.
Please report comments only when necessary. Unnecessary or abusive use of this tool may result in your own account being suspended.
Are you sure you want to delete your sketch?
Any files uploaded will be deleted as well.
Delete Comment?
This will also delete all the replies to this comment.
Delete this tab? Any code in it will be deleted as well.
Select a collection to submit your sketch
We Need Your Support
Since 2008, OpenProcessing has provided tools for creative coders to learn, create, and share over a million open source projects in a friendly environment.
Niche websites like ours need your continued support for future development and maintenance, while keeping it an ad-free platform that respects your data and privacy!
Please consider subscribing below to show your support with a "Plus" badge on your profile and get access to many other features!
CC Attribution ShareAlike
Spoon-Intersections
xxxxxxxxxx
var boolDoRefresh;
const NUM_LINES = 12;
function setup() {
createCanvas(720, 480);
background('#FFFFFF');
strokeWeight(5);
boolDoRefresh = true;
}
function draw() {
if(boolDoRefresh) {
background('#FFFFFF');
var lines = constructLines();
findIntersections(lines);
boolDoRefresh = false;
}
}
function mousePressed() {
boolDoRefresh = true;
}
function constructLines() {
const LINE_LENGTH = 250;
var lines = [];
for(var i = 0; i < NUM_LINES; i++) {
var x1 = random(720);
var y1 = random(480);
var x2 = random(100) > 50 ? x1 - random(LINE_LENGTH) : x1 + random(LINE_LENGTH);
var y2 = ((random(100) > 50 ? 1 : -1) * sqrt(sq(LINE_LENGTH) - sq(x1 - x2))) + y1;
line(x1, y1, x2, y2);
lines[i] = [x1, y1, x2, y2];
}
return lines;
}
function findIntersections(lines) {
//uses calculations found at http://paulbourke.net/geometry/pointlineplane/
//draws circles around any points where lines intersect
for(var i = 0; i < NUM_LINES; i++) {
for(var j = i + 1; j < NUM_LINES; j++) {
var x1 = lines[i][0];
var x2 = lines[i][2];
var y1 = lines[i][1];
var y2 = lines[i][3];
var x3 = lines[j][0];
var x4 = lines[j][2];
var y3 = lines[j][1];
var y4 = lines[j][3];
var u1 = (((x4 - x3) * (y1 - y3)) - ((y4 - y3) * (x1 - x3)))
/ (((y4 - y3) * (x2 - x1)) - ((x4 - x3) * (y2 - y1)));
var u2 = (((x2 - x1) * (y1 - y3)) - ((y2 - y1) * (x1 - x3)))
/ (((y4 - y3) * (x2 - x1)) - ((x4 - x3) * (y2 - y1)));
if(u1 > 0 && u1 < 1 && u2 > 0 && u2 < 1) {
var x = x1 + (u1 * (x2 - x1));
var y = y1 + (u1 * (y2 - y1));
fill('#FFC600');
ellipse(x, y, 15);
}
}
}
}
See More Shortcuts