“Tyvan-Intersections<br>” by TyVan
https://openprocessing.org/sketch/500884
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
Tyvan-Intersections<br>
xxxxxxxxxx
var numLines = 12;
var dia = 20;
var xOne, yOne, xTwo, yTwo;
var lines = [];
var inters = [];
// Properties related to whole document
function setup() {
createCanvas(720, 480);
noLoop();
ellipseMode(CENTER);
}
// Starts the first session
function draw() {
mousePressed();
}
// Refreshes everything on click
function mousePressed() {
background(235, 254, 240);
lines = [];
inters = [];
for(i = 0; i < numLines; i++) {
generateLine();
}
drawLines();
}
// Places x, y coordinates into matrix
function generateLine(){
var line = {
x1: random(width),
y1: random(height),
x2: random(width),
y2: random(height),
}
lines.push(line);
}
function drawLines() {
// Iterates through matrix of coordinates
for(i = 0; i < numLines; i++){
var x1 = lines[i].x1;
var y1 = lines[i].y1;
var x2 = lines[i].x2;
var y2 = lines[i].y2;
for(j = 0; j < numLines; j++){
var x3 = lines[j].x1;
var y3 = lines[j].y1;
var x4 = lines[j].x2;
var y4 = lines[j].y2;
LineIntersect(
x1, y1, x2, y2, x3, y3, x4, y4);
}
strokeWeight(1);
line(x1, y1, x2, y2);
}
for(i = 0; i < inters.length; i++){
mx = inters[i][0];
my = inters[i][1];
strokeWeight(0);
fill(255, 220, 220, 200);
ellipse(mx, my, dia, dia);
}
}
// Function adapted from Paule Bourke
// http://paulbourke.net/geometry/pointlineplane/pdb.c
function LineIntersect
(x1, y1, x2, y2, x3, y3, x4, y4) {
var mua,mub;
var denom,numera,numerb;
var tru;
denom = (y4-y3) * (x2-x1) - (x4-x3) * (y2-y1);
numera = (x4-x3) * (y1-y3) - (y4-y3) * (x1-x3);
numerb = (x2-x1) * (y1-y3) - (y2-y1) * (x1-x3);
var mx;
var my;
if (abs(denom) > 0){
/* Is the intersection along the segments */
mua = numera / denom;
mub = numerb / denom;
if (mua < 0 || mua > 1 || mub < 0 || mub > 1) {
return;
} else {
mx = x1 + mua * (x2 - x1);
my = y1 + mua * (y2 - y1);
inters.push([mx, my]);
}
}
}
See More Shortcuts