Created
June 25, 2011 09:03
-
-
Save frederickk/1046306 to your computer and use it in GitHub Desktop.
scriptographer scripts which pushes/pulls selected points
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
/** | |
* pointsPush/Pull 0.0 | |
* | |
* Ken Frederick | |
* ken.frederick@gmx.de | |
* | |
* http://cargocollective.com/kenfrederick/ | |
* http://kenfrederick.blogspot.com/ | |
* | |
*/ | |
// ------------------------------------------------------------------------ | |
// Properties | |
// ------------------------------------------------------------------------ | |
/** | |
* Note from the Scriptographer.org Team | |
* | |
* In Scriptographer 2.9, we switched to a top-down coordinate system and | |
* degrees for angle units as an easier alternative to radians. | |
* | |
* For backward compatibility we offer the possibility to still use the old | |
* bottom-up coordinate system and radians for angle units, by setting the two | |
* values bellow. Read more about this transition on our website: | |
* http://scriptographer.org/news/version-2.9.064-arrived/ | |
*/ | |
script.coordinateSystem = 'bottom-up'; | |
script.angleUnits = 'radians'; | |
// document properties | |
var objects; | |
var pointsCurr = new Array(); | |
// values | |
var values = { | |
thresh: 5, | |
random: 0 | |
}; | |
// gui components | |
var components = { | |
thresh: { | |
type: 'number', | |
label: 'distance threshold', | |
increment: 5, | |
steppers: true | |
}, | |
random: { | |
type: 'number', | |
label: 'random movement', | |
increment: 5, | |
steppers: true | |
}, | |
// ------------------------------------ | |
// apply that shit! | |
// ------------------------------------ | |
submit: { | |
type: 'button', | |
value: 'Apply', | |
onClick: function() { | |
Main(); | |
} | |
} | |
} | |
// ------------------------------------------------------------------------ | |
// Setup | |
// ------------------------------------------------------------------------ | |
function Setup() { | |
// initialize the palette window | |
var dialog = new Palette('points push/pull 0.0', components, values); | |
} | |
// ------------------------------------------------------------------------ | |
// Update | |
// ------------------------------------------------------------------------ | |
function Update() { | |
} | |
// ------------------------------------------------------------------------ | |
// Main | |
// ------------------------------------------------------------------------ | |
function Main() { | |
// document properties | |
//objects = activeDocument.getItems( { type: Item, selected: true } ); | |
objects = document.selectedItems.reverse(); | |
//gather points | |
for(var i=0; i<objects.length; i++) { | |
var obj = objects[i]; | |
if( obj.hasChildren() ) { | |
for(var k=0; k<obj.children.length; k++) { | |
gatherPoints( obj.children[k] ); | |
} | |
} else { | |
gatherPoints(obj); | |
} | |
} | |
//ammend points with new values | |
redefinePoints(); | |
//redistribute points | |
var index = 0; | |
for(var i=0; i<objects.length; i++) { | |
var obj = objects[i]; | |
if( obj.hasChildren() ) { | |
for(var k=0; k<obj.children.length; k++) { | |
updatePoints( obj.children[k] ); | |
} | |
} else { | |
updatePoints(obj); | |
/* | |
for(var j=0; j<obj.segments.length; j++) { | |
obj.segments[j].point = pointsCurr[index]; | |
index++; | |
//pointsCurr.push( pt ); | |
} | |
*/ | |
} | |
} | |
} | |
// ------------------------------------------------------------------------ | |
// Methods | |
// ------------------------------------------------------------------------ | |
function random(minr, maxr) { | |
return minr + Math.random() * (maxr - minr); | |
} | |
function comparePoints(pt1, pt2) { | |
if( dist(pt1,pt2) >= 0 && dist(pt1,pt2) <= values.thresh ) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
function dist(pt1, pt2) { | |
var dx = pt1.x - pt2.x; | |
var dy = pt1.y - pt2.y; | |
return Math.sqrt(dx*dx + dy*dy); | |
} | |
function gatherPoints(obj) { | |
for(var j=0; j<obj.segments.length; j++) { | |
var pt = obj.segments[j].point; | |
print(j + '\t' + obj.segments.length + '\t' + pt); | |
pointsCurr.push( pt ); | |
} | |
} | |
function redefinePoints() { | |
for(var i=0; i<pointsCurr.length; i++) { | |
var ptOld = pointsCurr[i]; | |
var ptNew = new Point( ptOld.x + random(-values.random,values.random), ptOld.y + random(-values.random,values.random) ); | |
for(var j=0; j<pointsCurr.length; j++) { | |
var d = dist(ptOld, pointsCurr[j]); | |
if( comparePoints(ptOld, pointsCurr[j]) ) { | |
//if( d < values.thresh ) { | |
pointsCurr[j] = ptNew; | |
} | |
} | |
} | |
} | |
function updatePoints(obj) { | |
for(var j=0; j<obj.segments.length; j++) { | |
print('pointsCurr.length', pointsCurr.length); | |
print(j + '\t' + pointsCurr.length % j); | |
//obj.segments[j].point = pointsCurr[ pointsCurr.length % j ]; | |
} | |
} | |
// ------------------------------------------------------------------------ | |
// Execution | |
// ------------------------------------------------------------------------ | |
Setup(); | |
Update(); | |
//Main(); |
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
/** | |
* pointsPushPullTool 0.0 | |
* | |
* Ken Frederick | |
* ken.frederick@gmx.de | |
* | |
* http://cargocollective.com/kenfrederick/ | |
* http://kenfrederick.blogspot.com/ | |
* | |
*/ | |
// ------------------------------------------------------------------------ | |
// Properties | |
// ------------------------------------------------------------------------ | |
/** | |
* Note from the Scriptographer.org Team | |
* | |
* In Scriptographer 2.9, we switched to a top-down coordinate system and | |
* degrees for angle units as an easier alternative to radians. | |
* | |
* For backward compatibility we offer the possibility to still use the old | |
* bottom-up coordinate system and radians for angle units, by setting the two | |
* values bellow. Read more about this transition on our website: | |
* http://scriptographer.org/news/version-2.9.064-arrived/ | |
*/ | |
script.coordinateSystem = 'bottom-up'; | |
script.angleUnits = 'radians'; | |
var sel; | |
var marker; | |
// values | |
var values = { | |
thresh: 500, | |
av: 0.5 | |
}; | |
// gui components | |
var components = { | |
thresh: { | |
type: 'number', | |
label: 'distance threshold', | |
increment: 5, | |
steppers: true | |
}, | |
av: { | |
type: 'number', | |
label: 'acceleration', | |
increment: 5, | |
steppers: true | |
} | |
} | |
// ------------------------------------------------------------------------ | |
// Setup | |
// ------------------------------------------------------------------------ | |
function Setup() { | |
// initialize the palette window | |
var dialog = new Palette('points push/pull 0.0', components, values); | |
} | |
// ------------------------------------------------------------------------ | |
// Update | |
// ------------------------------------------------------------------------ | |
function Update() { | |
} | |
// ------------------------------------------------------------------------ | |
// Main | |
// ------------------------------------------------------------------------ | |
function Main() { | |
} | |
// ------------------------------------------------------------------------ | |
// Methods | |
// ------------------------------------------------------------------------ | |
function random(minr, maxr) { | |
return minr + Math.random() * (maxr - minr); | |
} | |
function comparePoints(pt1,pt2) { | |
if( dist(pt1,pt2) >= 0 && dist(pt1,pt2) <= values.thresh ) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
function dist(pt1, pt2) { | |
var dx = pt1.x - pt2.x; | |
var dy = pt1.y - pt2.y; | |
return Math.sqrt(dx*dx + dy*dy); | |
} | |
// The infamous keepdist routine. Essentially relaxation. | |
// http://processing.org/discourse/yabb2/YaBB.pl?num=1270733327 | |
function keepDist(pt1, pt2, dist) { | |
var dx = pt2.x - pt1.x; | |
var dy = pt2.y - pt1.y; | |
var L = sqrt(dx*dx + dy*dy); | |
L = 0.5*(1-(D/L)); | |
var lx = L*dx; | |
var ly = L*dy; | |
pt1.x += lx; | |
pt1.y += ly; | |
pt2.x -= lx; | |
pt2.y -= ly; | |
} | |
// ------------------------------------------------------------------------ | |
// Events | |
// ------------------------------------------------------------------------ | |
function onMouseDown(event) { | |
sel = document.selectedItems.reverse(); | |
marker = new Path.Circle( event.point, values.thresh ); | |
} | |
function onMouseDrag(event) { | |
//show our distance | |
marker.position = event.point; | |
//go through the selected points | |
for( i in sel ) { | |
var obj = sel[i]; | |
//check if in a group | |
if( obj.hasChildren() ) { | |
for( k in obj.children ) { | |
movePoints( event, obj.children[k] ); | |
} | |
} else { | |
movePoints(event, obj); | |
} | |
} | |
//marker.remove(); | |
} | |
function onMouseUp(event) { | |
marker.remove(); | |
} | |
function movePoints(event, obj) { | |
//print(obj); | |
for( j in obj.segments ) { | |
var segPt = obj.segments[j].point; | |
var a = Math.atan2( segPt.y - event.point.y, segPt.x - event.point.x ); | |
var d = dist(event.point, segPt); | |
var m = values.thresh - d; | |
//if( comparePoints(event.point, segPt) ) { | |
if( d < values.thresh ) { | |
segPt.x += m * Math.cos(a) * values.av; | |
segPt.y += m * Math.sin(a) * values.av; | |
} | |
} | |
} | |
// ------------------------------------------------------------------------ | |
// Execution | |
// ------------------------------------------------------------------------ | |
Setup(); | |
Update(); | |
Main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment