Created
June 17, 2014 00:20
-
-
Save adacable/261ba535fa3d0c0d6dcf to your computer and use it in GitHub Desktop.
Print yourself a scarf.
This file contains hidden or 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
| import processing.pdf.*; | |
| //set up some constants | |
| Boolean pdfOut = true;//are we outputting to pdf or to screen? | |
| float averageDistance = 72*2 ;//how far between points, in inches/72 or pixels depending on output | |
| void setup() { | |
| background(255, 255, 255); | |
| if (pdfOut) { | |
| size(1684, 2383, PDF, "output.pdf"); | |
| } | |
| else { | |
| size(1684, 2383); | |
| } | |
| noStroke(); | |
| //stroke(255,255,255); | |
| fill(0, 0, 0); | |
| //noFill(); | |
| } | |
| void draw() { | |
| ArrayList<ArrayList<Point>> listOfPointLists = new ArrayList<ArrayList<Point>>(); | |
| listOfPointLists.add(makeStraightLine(0)); | |
| listOfPointLists.add(duplicateLineAt(listOfPointLists.get(0), 0)); | |
| for (int i=0; i<200;i++) { | |
| listOfPointLists.add(makeNextLine(listOfPointLists.get(i))); | |
| } | |
| ArrayList<Point> points = new ArrayList<Point>(); | |
| for (ArrayList<Point> currentList : listOfPointLists) { | |
| for (Point pointToAdd :currentList) { | |
| if (! (pointToAdd.outOfBounds())) { | |
| points.add(pointToAdd); | |
| } | |
| } | |
| } | |
| for (Point addThisPoint:duplicateLineAt(listOfPointLists.get(0),height)) { | |
| points.add(addThisPoint); | |
| } | |
| for (Point addThisPoint:makeVerticalLines()) { | |
| points.add(addThisPoint); | |
| } | |
| for (Point aPoint:points) {//itterate over primary points | |
| Point closestPoint = points.get(0); | |
| Point SecondClosestPoint = points.get(1); | |
| for (Point bPoint:points) {//iterate over secondary points | |
| if (bPoint.y > aPoint.y) {//if the secondary point is below the primary point | |
| if (aPoint.distanceTo(bPoint) < aPoint.distanceTo(SecondClosestPoint)) {//if the distance to the secondary point | |
| if (aPoint.distanceTo(bPoint) < aPoint.distanceTo(closestPoint)) { | |
| SecondClosestPoint = closestPoint; | |
| closestPoint = bPoint; | |
| } | |
| else { | |
| SecondClosestPoint = bPoint; | |
| } | |
| } | |
| } | |
| } | |
| //fill(255,255,255,aPoint.distanceTo(SecondClosestPoint)/2); | |
| if (closestPoint != points.get(0) && SecondClosestPoint != points.get(1) &&SecondClosestPoint != points.get(0)) { | |
| triangle(aPoint.x, aPoint.y, SecondClosestPoint.x, SecondClosestPoint.y, closestPoint.x, closestPoint.y); | |
| } | |
| } | |
| noLoop(); | |
| if (pdfOut) { | |
| exit(); | |
| } | |
| else { | |
| noLoop(); | |
| } | |
| } | |
| ArrayList<Point> makeNextLine(ArrayList<Point> prevLine) { | |
| ArrayList<Point> pointsToReturn = new ArrayList<Point>(); | |
| float currentX = 0; | |
| while (currentX < width) {//while we need new points | |
| float newX = currentX; | |
| Point nearestPointByX = prevLine.get(0);//make the nearest point the first one | |
| for (Point pointToTest : prevLine) { | |
| if (abs(pointToTest.x - newX) > abs(nearestPointByX.x - newX)) { | |
| nearestPointByX = pointToTest; | |
| } | |
| } | |
| float newY = nearestPointByX.y + random(2)*averageDistance; | |
| //add a random number to that points Y and store it in newY | |
| Point newPoint = new Point(newX, newY);//make the new point | |
| newPoint.drawThis(); | |
| pointsToReturn.add(newPoint);//add the new point | |
| currentX = currentX + random(2)*averageDistance/4;//make a new point about averageDistance along | |
| } | |
| return pointsToReturn;//return dem points | |
| } | |
| ArrayList<Point> makeStraightLine(float yIn) { | |
| ArrayList<Point> pointsToReturn = new ArrayList<Point>(); | |
| float currentX = 0; | |
| while (currentX < width) { | |
| pointsToReturn.add(new Point(currentX, yIn)); | |
| currentX = currentX + random(2)*averageDistance; | |
| } | |
| return pointsToReturn; | |
| } | |
| ArrayList<Point> duplicateLineAt(ArrayList<Point> lineIn, float yIn) { | |
| ArrayList<Point> lineOut = new ArrayList<Point>(); | |
| for (Point toMovePoint :lineIn) { | |
| Point movedPoint = new Point(toMovePoint.x, yIn); | |
| lineOut.add(movedPoint); | |
| } | |
| return lineOut; | |
| } | |
| ArrayList<Point> makeVerticalLines() { | |
| ArrayList<Point> pointsToReturn = new ArrayList<Point>(); | |
| float currentY = 0; | |
| while (currentY < height) { | |
| pointsToReturn.add(new Point(0, currentY)); | |
| pointsToReturn.add(new Point(width, currentY)); | |
| currentY = currentY + random(2)*averageDistance; | |
| } | |
| return pointsToReturn; | |
| } | |
| class Point { | |
| String toString() { | |
| return "("+ x +"," + y +")"; | |
| } | |
| float x; | |
| float y; | |
| int count; | |
| Point(float xIn, float yIn ) { | |
| x = xIn; | |
| y = yIn; | |
| } | |
| float distanceTo(Point secondPoint) { | |
| return sqrt(sq(x - secondPoint.x) + sq(y - secondPoint.y)); | |
| } | |
| void drawThis() { | |
| //ellipse(x,y,10,10); | |
| } | |
| Boolean outOfBounds() { | |
| if ((x > width - 5 ||y>height -5||x<5)) { | |
| return true; | |
| } | |
| else { | |
| return false; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment