-
1. Re: height parameter and math.random constraint
CarlosCanto Apr 25, 2012 1:47 PM (in response to Petros_)I made a quick search and found a function to get a random number.
Use Resize to change the height instead of changing the height property, that'll take care of problem B.
//get all the objects that are currently selected in the document var selectedObjects = app.activeDocument.selection; //decrease their size by a random number between 0% and 70% for (var i=0; i<selectedObjects.length; i++){ //get the scale (random number between 30-100) var scale = randomXToY (30, 100); // scale Y only, from top selectedObjects[i].resize (100, scale, true, true, true, true, 1, Transformation.TOP) } //function to get random number upto m, by Roshan Bhattarai function randomXToY(minVal,maxVal,floatVal) { var randVal = minVal+(Math.random()*(maxVal-minVal)); return typeof floatVal=='undefined'?Math.round(randVal):randVal.toFixed(floatVal); } //alert(randomXToY (30, 100));
for problem [A], I would use a different approach. I would start with 1 object (since they're all the same) and duplicate it, scale it, and rotate it as many times as I need it to complete the circle. We can use a fixed point using these methods.
-
2. Re: height parameter and math.random constraint
Petros_ Apr 25, 2012 2:21 PM (in response to CarlosCanto)Thanks Carlos! And thanks Roshan! As to Problem A, my hope is to come up with a more generic script that I could reuse in different scenarios within Illustrator. For example I might not always want to rotate the object. I prefer to be able to control the shape, size, rotation angle, and rotation point manually in Illustrator and then do this transformation. Is there a way to easily access the anchor point that I've defined for a given object?
-
3. Re: height parameter and math.random constraint
CarlosCanto Apr 25, 2012 2:39 PM (in response to Petros_)yes, the rotateAbout or scaleAbout or transformAbout all refer to those 9 points (I used TOP in the previous example), plus the document origin, which is the (0,0) you can change that manually or via script
Transformation
BOTTOM, LEFT
BOTTOMLEFT, RIGHT
BOTTOMRIGHT, TOP
CENTER, TOPLEFT
DOCUMENTORIGIN, TOPRIGHT
edit:
I re-read your previous question, the problem with rotating your object before you scale it is that it will get skewed like in you example. Scaling is only vertical and/or horizontal, you need to scale before rotate.
Message was edited by: CarlosCanto
-
4. Re: height parameter and math.random constraint
Petros_ Apr 25, 2012 2:58 PM (in response to CarlosCanto)I tried inserting BOTTOM instead of TOP, but it's still resizing based off an (imaginary to me) bounding box around the rotated rectangles. Doesn't Illustrator track the rotation angle of an object after it has been rotated? For instance, if I make a rectangle and rotate 30 degrees, doesn't Illustrator know that the object is actually a rectangle rotated 30 degrees?
On a related note, do you know where I can find information about the "true", "1", etc. values?:
selectedObjects[i].resize (100, scale, true, true, true, true, 1, Transformation.TOP)
Thanks again!
-
5. Re: height parameter and math.random constraint
CarlosCanto Apr 25, 2012 3:46 PM (in response to Petros_)I tried inserting BOTTOM instead of TOP, but it's still resizing based off an (imaginary to me) bounding box around the rotated rectangles
try using BOTTOM on an unrotated item, to see how it works first, as I said, resize first, then rotate, otherwise shapes will not look right.
doesn't Illustrator know that the object is actually a rectangle rotated 30 degrees?
no, it just looks at the bounding box of a rotated or un-rotated objects.
do you know where I can find information about the "true", "1", etc. values?:
1. Scripting Reference
2. Object Model Viewer
they're the parameters to scale patterns, gradients, strokes, etc.
-
6. Re: height parameter and math.random constraint
Petros_ Apr 25, 2012 4:30 PM (in response to CarlosCanto)What if instead of rectangles I used stroked lines? Could I then control the line length after I've rotated it?
-
7. Re: height parameter and math.random constraint
CarlosCanto Apr 25, 2012 5:30 PM (in response to Petros_)is there a reason you MUST rotate first?
-
8. Re: height parameter and math.random constraint
Petros_ Apr 25, 2012 7:33 PM (in response to CarlosCanto)The number of items I'm going to duplicate in a circle and at what angle and what reference point varies just about every time I duplicate objects around a point (I'm using alt+click with rotate tool and ctrl+D to create the rotated objects). I usually rely heavily on my eyes to figure out what angle to rotate things. If I encode all that in the script, I'll be forced to modify the script in a trial-and-error process every time I want to use the script on a set of rotated objects. Hope that makes sense, I really appreciate the help you've offered.
-
9. Re: height parameter and math.random constraint
CarlosCanto Apr 26, 2012 12:54 AM (in response to Petros_)you lost me, I don't understand what you need. If you have to vary the rotation then no script can help you. If you have to rely on your eyes...same thing, that can't be scripted either.
post a real example of what you need, with pretty pictures like you did in your fist post, we might come up with alternatives.
-
10. Re: height parameter and math.random constraint
Petros_ Apr 26, 2012 11:43 AM (in response to CarlosCanto)Here's an example, which isn't that hard to make manually, but it would be fun if I could have the flexibility to make this more quickly with more rectangles (or lines), more rings of circles, yet retain direct control (in the UI) over the exact sizes of the rectangles moving out from the center white circle.
If I wanted to make more rings, different sizes, with more rectangles (or lines) right now I would have to manually adjust each rectangle length (which I don't want to do, tedious and long, and not very creative). With the script I could create a rectangle (or line), dupe it a bunch of times around a rotation point, and then run the script to vary how far each object scales in line with the center point.
-
11. Re: height parameter and math.random constraint
Muppet Mark Apr 26, 2012 12:45 PM (in response to Petros_)1 person found this helpfulPetros, Carlos is right in the fact that this is much easier if you…
dupe rectangle ( front/back what ever )
scale the duplicate while verticle or horizontal
rotate about some origin
The other opition is to calculate all the points with math and create the paths… That's Jong business…
-
12. Re: height parameter and math.random constraint
CarlosCanto Apr 26, 2012 11:52 PM (in response to Muppet Mark)1 person found this helpfulcalculating all the points is just overkill, specially when there's really no need for it.
Petros, here's a sample of the same thing we've been talking about, scale first, rotate next.
move your key object close to the (0,0), actually right above the (0,0) on the y axis or (0,radius) before running.
//select ONE object before running var idoc = app.activeDocument; var copies = 20; var angle = 360/copies; var igroup = idoc.groupItems.add(); var sel = idoc.selection[0]; sel.move (igroup, ElementPlacement.PLACEATBEGINNING); var colorRef = new RGBColor; colorRef.red = 255; var ipath = igroup.pathItems[0]; for (var i=0; i<copies; i++){ var idup = ipath.duplicate(); //get the scale (random number between 30-100) var scale = randomXToY (0, 70); $.writeln(scale); // scale Y only, from top idup.resize (100, scale, true, true, true, true, 1, Transformation.BOTTOM); idup.rotate(angle*i,true,true,true,true,Transformation.DOCUMENTORIGIN); idup.fillColor = colorRef; } ipath.hidden = true; //function to get random number upto m, by Roshan Bhattarai function randomXToY(minVal,maxVal,floatVal) { var randVal = minVal+(Math.random()*(maxVal-minVal)); return typeof floatVal=='undefined'?Math.round(randVal):randVal.toFixed(floatVal); }
-
13. Re: height parameter and math.random constraint
Muppet Mark Apr 27, 2012 3:54 AM (in response to CarlosCanto)Yep thats almost the exact same way I tested… as long as the starting shape is vertial or horizontal then it won't become a shear when scaled…
-
14. Re: height parameter and math.random constraint
[Jongware] Apr 27, 2012 5:04 PM (in response to Petros_)(Just dropping in on something only slightly related)
Your original random scaling factor fails because your random is no good. Instead of this
h = Math.floor((Math.random()*myHeights)+.3);
you should have used this
h = Math.floor(myHeights*(0.3 + 0.7*Math.random()));
That gives you a nice spread between 30% and 100% of the original heights.