Processing Month, Day 2 - Connecting Points, Part 2
Today’s sketch will be similar to the one from yesterday, only we’ll use random points and connect them in a different way. We will draw a line if the distance between the points is less than a certain number. The distance will also be used as the value for the transparency of the line. Points close to each other will have a less transparent connection between them, points that are further away will be connected by a very transparent line.
To calculate the distance between points, we’ll use the dist() function. First two parameters are for the x and y coordinates for the first point, the third and fourth parameter take the x and y coordinates for the second point. The method returns the distance between the two points as a float. If this value is less than 255, we will draw the line.
float dst = dist( points<i>.x, points<i>.y, points[j].x, points[j].y );
if ( dst < 255 ) {
stroke( 255, 255 - dst );
line( points<i>.x, points<i>.y, points[j].x, points[j].y );
}
Once we have drawn the fine lines between the points, we need to draw the points themselves, with a bigger stroke weight. This will make our final image look a lot better. So this code will be added at the end the first for-loop, right after the inner loop.
stroke( 255 );
strokeWeight(4);
point( points<i>.x, points<i>.y );
The Results




Documentation
Here are some links to the Processing documentation for the functions I used. The might be handy if you need some extra information.
Download
Related Articles
Browse Articles
Comments (4)
From:Dan Bernier
Date:04.05.2011
Very nice – it’s a good 3D effect in 2D.
One trick I like to use for static sketches like this is to put all the work in draw(), add noLoop() at the bottom of draw(), and add a mouseClick handler:
void mouseClicked() {
loop();
}
This way, you can just click to re-run the sketch.
BTW, for anyone who missed the earlier post, you can see this sketch in action at http://www.openprocessing.org/visuals/?visualID=28023.
From:Simon
Date:05.05.2011
This is excellent, thanks for sharing it.
From:Ian Bowman
Date:05.05.2011
I really like these Processing Month sketches. They are informative, yet concise. I’ve expanded upon your Sketch 2 idea by adding animation and an image generation component: http://ibowman.loni.ucla.edu/2011/05/04/processing-month-day-2-—-remix/
Popular Articles
- Introduction to openFrameworks
- Creating 3D Shapes with Hemesh
- Mirroring Video with openFrameworks
- An Introduction to colorLib
- How to create a FullScreen iPhone Application
Popular Tags
- processing (95)
- software (50)
- art (48)
- web design (40)
- photography (39)
From:Jan Vantomme
Date:04.05.2011
Thanks for the tip to regenerate static sketches. Might be useful for other readers.
Top · Permanent link to this comment