Sg2.9
get Rotation of already rotat...
Recent RSS
get Rotation of already rotated object
From:  Thijs Gadiot
Date:  23. February 2008, 23:05

Hey Scriptographers,

I'm looking for a way in which I can get the rotation value of an object (path), that was rotated by hand in illustrator. I'm not sure if this is possible, so I was thinking of using squares as objects, and trying to calculate the angle in which it was rotated (using the center of the object as the pivot point) by comparing the relative position of the points towards each other.

Any ideas? Thanks in advance!

Re: get Rotation of already rotated object
From:  gs
Date:  27. February 2008, 23:37

it does not work in general, but it is a starting point:

//
// only test code...
// does only work in a few cases...
// g.s
//

var obj = document.selectedItems[0];

var center = obj.bounds.center;
var corner1 = obj.curves[0].point1;
var corner2 = obj.curves[1].point1;

var refPoint = new Point(corner2.x,corner1.y);

print(corner1);
print(corner2);

document.createCircle(corner1, 3);
document.createCircle(corner2, 3);
document.createCircle(refPoint, 3);

var ak = refPoint.getDistance(corner2);
var gk = refPoint.getDistance(corner1);

var tan = gk / ak;
var angle = Math.atan(tan);

print(angle);

var refRect = document.createRectangle(Rectangle(0, 0, 50, 50));
refRect.rotate(angle,refRect.bounds.center);

look at these for some explanation: http://img518.imageshack.us/img518/9151/rotatell2.gif

i think there is a better way to go, but this was my first idea how to solve the problem.
i'd like to see other solutions...

greetings
g.s

Re: get Rotation of already rotated object
From:  Thijs Gadiot
Date:  1. March 2008, 12:58

Thanks alot for your reply, this is the solution I've come up with, and it is working well for me, in every angle.

sel = activeDocument.getMatchingItems(Path,{selected:true});

function getPoint(i){
	// get the point of the rectangle that was originally the upper left one
	return(sel[i].segments[2].point);
}


function drawBaseLine(i){
	// draw a new path displaying the baseline
	baseline = new Path();
	baseline.segments.add(new Point(sel[i].bounds.center.x-75,sel[i].bounds.center.y));
	baseline.segments.add(new Point(sel[i].bounds.center.x+75,sel[i].bounds.center.y));
}

function graden(){
	// calculate the angle in which the corner point is compared
	// to the centerpoint of the square.
		
	xDiff = Math.abs(sel[0].bounds.center.x - getPoint(0).x);
	yDiff = Math.abs(sel[0].bounds.center.y - getPoint(0).y);
				
	var thisTan = yDiff / xDiff;
	var angle = Math.atan(thisTan);
	
	graden = angle * (180 / Math.PI);
	
	return(graden);
}

// draw a circle to indicate which of the four points
// was originally in the upper left corner
activeDocument.createCircle(getPoint(0), 15);
drawBaseLine(0);


if (getPoint(0).x < sel[0].bounds.center.x){
	
	// figure out in which area the corner point is
	// compared to the center point of the square, and
	// add the appropriate amount of graden to the
	// result of the tangens to get the value of the
	// rotation.
	
	if (getPoint(0).y > sel[0].bounds.center.y){
		print ("upper left corner: ");
		result = graden()-45;
		if (result < 0){
			result += 360;
		}
		print ("rotation: " + Math.round(result) + " graden");

	}
	else {
		print ("lower left corner");
		result = (90 - graden()) + 225;
		print ("rotation: " + Math.round(result) + " graden");
	}
}

else if (getPoint(0).x > sel[0].bounds.center.x){
	
	if (getPoint(0).y > sel[0].bounds.center.y){
		print ("upper right corner");
		result = (90 - graden()) + 45;
		print ("rotation: " + Math.round(result) + " graden");
			
	}
	else {
		print ("lower right corner");
		result = graden()+135;
		print ("rotation: " + Math.round(result) + " graden");
	}
}

And an image of some action, click link for full image:
http://www.thijsgadiot.nl/calcAngle.png

Scripts
08.08.14, 15:24
15.05.14, 14:23
02.03.14, 19:16
18.11.13, 14:48
22.03.13, 03:05
22.02.13, 15:45
Posts
10.01.17, 16:37
19.02.16, 06:03
19.02.16, 06:00
17.01.16, 11:00
12.01.16, 13:10
25.11.15, 08:19
Script of the Moment
Parallel Arrangement 07.04.12