Tuesday, April 28, 2015

drawing - Illustrator: How to draw circles at control points?


I drew a polygon using the pen tool. Now I'd like to draw small circles (illustrating vertices) at the polygon control points.



Answer



As per request, a script to do this on selected paths. This is an alternative for @CConroy answer that may make things easier to do in the long run. It draws symbols on points and tangents and connects the tangents with lines, You can use symbols palette to change how they look after the fact. Might be useful for somebody.


Put following in a .jsx file and run it with paths selected:


#target illustrator

main();


function main(){
var sym = createSymbolsIfNeeded(activeDocument);
handlePaths(activeDocument.selection,
sym.anchor, sym.tangent);
// pass undefined for sym tangent if you dont want
// tangent handles.
}

function handlePaths(sel, symAcnhor, symDir) {

for(var i = 0; i < sel.length; i++){
if(sel[i].typename == "PathItem"){
symbolsOnPoints(sel[i], symAcnhor, symDir);
}
// you would possibly need to recurse
// the groups and compound paths in some
// cases.
}
}


function symbolsOnPoints(path, symAcnhor, symDir) {
var pts = path.pathPoints;
for(var i = 0; i < pts.length; i++){
var pos = pts[i].anchor

if(!(symDir === undefined)) {
var pos2 = pts[i].rightDirection;
if (dist(pos, pos2) > 0.1){
drawLine(pos, pos2);
centeredSymbol(pos2, symDir);

}

pos2 = pts[i].leftDirection;
if (dist(pos, pos2) > 0.1){
drawLine(pos, pos2);
centeredSymbol(pos2, symDir);
}
}

centeredSymbol(pos, symAcnhor);

}
}

function centeredSymbol(pos, symbol) {
var p = pos.slice(0);
sym = activeDocument.symbolItems.add(symbol);
p[0] -= sym.width/2.0;
p[1] += sym.height/2.0;
sym.position = p;
return sym;

}

function drawLine(p1, p2) {
var line = activeDocument.pathItems.add();
line.setEntirePath([p1,p2]);
return line;
}

function dist(a, b){


return Math.sqrt(Math.pow(b[0]-a[0], 2) +
Math.pow(b[1]-a[1], 2));
}

function createSymbolsIfNeeded(doc){
var symAnchor;
try {
symAnchor = doc.symbols.getByName("Anchor");
}
catch(err) {

var circle = doc.pathItems.ellipse(5,5,5,5);
symAnchor = doc.symbols.add(circle);
symAnchor.name = "Anchor";
circle.remove();
}

var symTangent;
try {
symTangent = doc.symbols.getByName("Tangent");
}

catch(err) {
var rect = doc.pathItems.rectangle(5,5,5,5);
var symTangent = doc.symbols.add(rect);
symTangent.name = "Tangent";
rect.remove();
}
return {anchor : symAnchor, tangent: symTangent};
}

    tangents



Image 1: Example result. You can easily change the different marker symbols win the symbol palette.


PS: The script is not really super secure and stable because its one of my own scripts for quick documentation so be careful.


PPS: I have pushed this code to bitbucket


No comments:

Post a Comment

technique - How credible is wikipedia?

I understand that this question relates more to wikipedia than it does writing but... If I was going to use wikipedia for a source for a res...