Skip to main content
Geppetto Luis
Legend
April 4, 2020
Answered

Random circles

  • April 4, 2020
  • 4 replies
  • 5646 views

I would like to create white circles in my document
the size of the circle and the position must be random

an example of what I'm looking for I used a black background
to see better what I'm looking for.

 

Correct answer c.pfaffenbichler
The below code produces something like this, to amend the number, colors and radii of the circles amend the line
createCircle(Math.random()*theWidth, Math.random()*theHeight, 10+Math.random()*90, Math.random()*255, Math.random()*255, Math.random()*255);
if (app.documents.length > 0) {
var myDoc = app.activeDocument;
var theWidth = myDoc.width;
var theHeight = myDoc.height;
for (var m = 0; m < 20; m++){
createCircle(Math.random()*theWidth, Math.random()*theHeight, 10+Math.random()*90, Math.random()*255, Math.random()*255, Math.random()*255);
};
};
//////////////////
function createCircle (theCenterX, theCenterY, theRadius, theR, theG, theB) {
var idpixelsUnit = stringIDToTypeID( "pixelsUnit" );
var idmake = stringIDToTypeID( "make" );
var desc15 = new ActionDescriptor();
var idnull = stringIDToTypeID( "null" );
var ref4 = new ActionReference();
var idcontentLayer = stringIDToTypeID( "contentLayer" );
ref4.putClass( idcontentLayer );
desc15.putReference( idnull, ref4 );
var idusing = stringIDToTypeID( "using" );
var desc16 = new ActionDescriptor();
var idtype = stringIDToTypeID( "type" );
var desc17 = new ActionDescriptor();
var idcolor = stringIDToTypeID( "color" );
var desc18 = new ActionDescriptor();
var idred = stringIDToTypeID( "red" );
desc18.putDouble( idred, theR );
var idgrain = stringIDToTypeID( "grain" );
desc18.putDouble( idgrain, theG );
var idblue = stringIDToTypeID( "blue" );
desc18.putDouble( idblue, theB );
var idRGBColor = stringIDToTypeID( "RGBColor" );
desc17.putObject( idcolor, idRGBColor, desc18 );
var idsolidColorLayer = stringIDToTypeID( "solidColorLayer" );
desc16.putObject( idtype, idsolidColorLayer, desc17 );
var idshape = stringIDToTypeID( "shape" );
var desc19 = new ActionDescriptor();
var idunitValueQuadVersion = stringIDToTypeID( "unitValueQuadVersion" );
desc19.putInteger( idunitValueQuadVersion, 1 );
var idtop = stringIDToTypeID( "top" );
desc19.putUnitDouble( idtop, idpixelsUnit, theCenterY - theRadius );
var idleft = stringIDToTypeID( "left" );
desc19.putUnitDouble( idleft, idpixelsUnit, theCenterX - theRadius );
var idbottom = stringIDToTypeID( "bottom" );
desc19.putUnitDouble( idbottom, idpixelsUnit, theCenterY + theRadius );
var idright = stringIDToTypeID( "right" );
desc19.putUnitDouble( idright, idpixelsUnit, theCenterX + theRadius );
var idellipse = stringIDToTypeID( "ellipse" );
desc16.putObject( idshape, idellipse, desc19 );
var idcontentLayer = stringIDToTypeID( "contentLayer" );
desc15.putObject( idusing, idcontentLayer, desc16 );
var idlayerID = stringIDToTypeID( "layerID" );
desc15.putInteger( idlayerID, 3 );
executeAction( idmake, desc15, DialogModes.NO );
};

4 replies

Chuck Uebele
Community Expert
Community Expert
April 5, 2020

Yea, I do a lot of images using scripts to place random shapes. Mine have gradients that are applied by sampling a reference layer.

JJMack
Community Expert
Community Expert
April 5, 2020

I see a Ghost, a Man, a Dog and a Fish.  You did create a colorful Rorschach test Chuck.

JJMack
Kukurykus
Legend
April 5, 2020

Not fish, but turtle.

jane-e
Community Expert
Community Expert
April 4, 2020

Hi

 

I love C_P's scripting solution!

 

If you have Illustrator, though, it's super simple.

  1. Draw your shape, either the circle or the polygon or anything else.
    Drag it into symbols to make a symbol.
    Delete the shape from the artboard
  2. Use the Symbol Sprayer tool to spray a symbol set.
    Use the Symbol Sizer tool to make the shapes larger (add Option to do the opposite)
    Use the Symbol Shifter tool to scoot them around.
  3. If you start with a circle and then want the polygon, create a new symbol and use the replace symbol command to update wherever the symbol is in use.

 

If you think you will have edits and customization, you might want to use Illustrator for this project.

 

~ Jane

c.pfaffenbichler
Community Expert
Community Expert
April 5, 2020

Come to think of it I tinkered with a Script a couple years ago to distribute Smart Object instances based on a Channel. 

Using Smart Objects allows for summary editing later on. 

c.pfaffenbichler
Community Expert
Community Expert
April 4, 2020

This is an example of a Brish randomized Shape Dynamics and Scattering applied to a straight Path. 

Geppetto Luis
Legend
April 4, 2020

I have tried this method m is not good for me

c.pfaffenbichler
Community Expert
Community Expert
April 4, 2020

Could you elaborate without having to be asked to each time? 

c.pfaffenbichler
Community Expert
Community Expert
April 4, 2020

So what is the question or problem? 

 

You could use a randomized Brush with Scattering or a Script. 

Geppetto Luis
Legend
April 4, 2020

if you can a ui dialog
if it is too much a script is also fine.

c.pfaffenbichler
Community Expert
Community Expert
April 4, 2020

What do you mean? 

Please elaborate. 

And if you want to pursue a Scripting-approach: Do you have any JavaScript-experience?