Problem with Negative Angle Rotation

Upload Area - Upload Files Here, link to them from the appropriate Forum

Moderators: Tom, Kukurykus

soorya

Problem with Negative Angle Rotation

Post by soorya »

How to find the correct angle for the attached rotated rectangles ?
How to find the anchor point of the circled corners ?

Pls. note the circled anchor points. Which are varying for Negative rotated rect.

soorya
Mike Hale

Problem with Negative Angle Rotation

Post by Mike Hale »

One of the problem is that the Abode fourm mangled the code I posted so that the test for a egative angle was in a comment. It should be

Code: Select allmeasureLine = function(path){
   var res = new Array;
   var lineStart = path.subPathItems[0].pathPoints[0].anchor;
   var lineEnd = path.subPathItems[0].pathPoints[1].anchor;
   var a = Math.max(lineStart[0],lineEnd[0])-Math.min(lineStart[0],lineEnd[0]);
   var o = Math.max(lineStart[1],lineEnd[1])-Math.min(lineStart[1],lineEnd[1]);
   var c = Math.round(Math.sqrt((a*a)+(o*o)));
   res.push(c);
   var ang = (180/Math.PI) * Math.atan2(o,a);
   if(lineStart[1] < lineEnd[1]){
       ang = -ang;
   };
   res.push(ang); return res;
}
function selectVectorMask(){
    var desc = new ActionDescriptor();
        var ref = new ActionReference();
        ref.putEnumerated( charIDToTypeID( "Path" ), charIDToTypeID( "Path" ), stringIDToTypeID( "vectorMask" ) );
        ref.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );
    desc.putReference( charIDToTypeID( "null" ), ref );
executeAction( charIDToTypeID( "slct" ), desc, DialogModes.NO );
};

selectVectorMask()
   var p = activeDocument.pathItems[0]
   var angle = measureLine(p)[1];
alert(angle)

The image you posted only has normal rectangles as the vector masks. But the code should work with rounded rectangles made with the shape tool as well.
It will not work with most of the other shapes or with paths converted from a selection.

Mike
soorya

Problem with Negative Angle Rotation

Post by soorya »

Thanks again Mike,

I think I use the similar code, which you have sent to me long back.
But I face some problem in that. Pls. try the script in the attached psd file
and get angles for all the 4 vector layers. You may get correct angles for
Top 2 rectangles and may get wrong angles for the bottom layers.

Doc.PathItems(1).SubPathItems(1).PathPoints(1).anchor(0)

The problem is, I get the correct (LeftTop corner) points for the Top Rect
and similar Rect on the bottom I get TopRight corner points.
So the angle becomes wrong.

How to reset the Path Points to the LeftTop corner for any angled Rect ?

soorya
Mike Hale

Problem with Negative Angle Rotation

Post by Mike Hale »

The script shows the two top shapes were rotated +/- 18 deg and the bottom two +/- 15 deg.

If I transform-rotate each path using the value returned by the function they are set back to the unrotated shape.

What values are you getting and why do you think those values are wrong?

Mike
soorya

Problem with Negative Angle Rotation

Post by soorya »

I get Top Left Rect = +18 (PLUS) which is correct and
Bottom Left Rect = -15 (MINUS) which supposed to be Plus 15

Pls. note all the circled corners. Which is the correct lineStart[0]
But I get correct on the Top Left Rect and
I get wrong on the Bottom Left Rect.
I get RightBottom corner as lineStart[0] so

if(lineStart[1] < lineEnd[1]){//negative angle ang = -ang; };
res.push(ang); return res; }

Makes Oppsite Direction of Angle.

I need the correct angle for both the cases and I need the Correct Anchor
Points also.

soorya
Mike Hale

Problem with Negative Angle Rotation

Post by Mike Hale »

I think I see the problem. The function will not work if the shape was rotated more than +/- 90 deg. With a rectangle there is really no reason to rotate more than +/- 90 but the code below seems to correct the error is it has been. Note that I did not test all the angles so it still may fail with some angles.

Code: Select allmeasureLine = function(path){
   var res = new Array;
   var lineStart = path.subPathItems[0].pathPoints[0].anchor;
   var lineEnd = path.subPathItems[0].pathPoints[1].anchor;
   var a = Math.max(lineStart[0],lineEnd[0])-Math.min(lineStart[0],lineEnd[0]);
   var o = Math.max(lineStart[1],lineEnd[1])-Math.min(lineStart[1],lineEnd[1]);
   var c = Math.round(Math.sqrt((a*a)+(o*o)));
   res.push(c);
   var ang = (180/Math.PI) * Math.atan2(o,a);
   if(lineEnd[1] > lineStart[1] || lineEnd[0] < lineStart[0]){
       ang = -ang;
   };
   res.push(ang); return res;
}
function selectVectorMask(){
    var desc = new ActionDescriptor();
        var ref = new ActionReference();
        ref.putEnumerated( charIDToTypeID( "Path" ), charIDToTypeID( "Path" ), stringIDToTypeID( "vectorMask" ) );
        ref.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );
    desc.putReference( charIDToTypeID( "null" ), ref );
executeAction( charIDToTypeID( "slct" ), desc, DialogModes.NO );
};

selectVectorMask()
   var p = activeDocument.pathItems[0]
   var angle = measureLine(p)[1];

Mike
soorya

Problem with Negative Angle Rotation

Post by soorya »

Mike,
I am sorry to trouble you a lot.

Since I am converting your JS code to VB6 there must be somthing wrong.
But so far I am sure that, I have'nt done any mistakes in conversion, and
the code works how I expect.

Pls. have a look at the new attached image Rot3.psd
Observe the LineStart and LineEnd, which got changed the other way in image B.

So the below checking code is not working the way it should.
As per my knowledge the LineStart and LineEnd must not get changed.
If so, we can easily check the angles
Due to some reason if it get changed, how to reset it ?

Code: Select allif(lineEnd[1] > lineStart[1] || lineEnd[0] < lineStart[0]){
       ang = -ang;
   };


So in both A and B we get
LineEnd[1] > LineStart[1]

And this is a small sample image to clear the basic understandings about
PathPoints. But my actual application is facing many issues closer to this.
Once I get clear with the basics, I would fix those.

I think, we have to re create the Path for the ShapeLayer (VectorMask)
with the correct Starting Point. So that we can get correct angle for any Rect shape layers.

But when I tried to re create the path, The same basic error is followed in the new layer too.

Note: pls. try Rot4.psd too

soorya
Mike Hale

Problem with Negative Angle Rotation

Post by Mike Hale »

soorya wrote:Observe the LineStart and LineEnd, which got changed the other way in image B.
I thought that was because the shape was rotated more than +/- 90 deg and the pathpoint is not where the script expects it. But I can't rotate a shape with any angle so that it matches your example. However if I transform-flip horzontal I can. The function can't deal with a shape transformed that way.

So the below checking code is not working the way it should.
As per my knowledge the LineStart and LineEnd must not get changed.
If so, we can easily check the angles
Due to some reason if it get changed, how to reset it ?
The pathpoints order was defined when the path was created. You can't change that order. You can change the value of the anchor for a path, that is what happens when the path is transformed.

The function assumes that the top left pathpoint is the first pathpoint and that the second pathpoint is the top right. The code below was a simple test to correct the sign of the angle if the pathpoints where not where expected.

Code: Select allif(lineEnd[1] > lineStart[1] || lineEnd[0] < lineStart[0]){
       ang = -ang;
   };


So in both A and B we get
LineEnd[1] > LineStart[1]

Yes that is true but || is javascript for OR so it also test to see if 'the line ends before it starts' which means the pathpoint order is wrong. As I noted I didn't think that test would work for all cases.

I think, we have to re create the Path for the ShapeLayer (VectorMask)
with the correct Starting Point. So that we can get correct angle for any Rect shape layers.

I suggest that when you recreate the templates that you only rotate them and then only +/- 90 deg. Don't flip them. To help you understand what happens to the pathpoints when a shape is transformed I have attached a shape with a check mark at point 0 and an arrow at point 1.

I should also point out that the function returns the angle of the line not the angle the shape was transformed. In your rot3 example you have the signs inverted. The angle of the top of shape A is -15

Mike
soorya

Problem with Negative Angle Rotation

Post by soorya »

Thanks a million Mike,
Very clear explanation...
But I still have some doubts.

1)

Code: Select allif(lineEnd[1] > lineStart[1] || lineEnd[0] < lineStart[0]){
       ang = -ang;
   };


Before the above condition, what would be the sign of (ang) a +15 Rotated Rect ? I get just +15
Where as I could guess from your chat, that it returns -15 and then the above code makes it +15 to give the final actual sign and value.

I should also point out that the function returns the angle of the line not the angle the shape was transformed. In your rot3 example you have the signs inverted. The angle of the top of shape A is -15


Even you have said that shape A in rot3.psd is -15 for you. But it is +15 for me and that is what I have made while designing.

As per my knowledge the LineStart[1] must be < LineEnd[1] for +15,
because if point 0 starts from Y axis zero then Point 1 Y axis should go down, that means the Y may be 100.
So here LineStart[1] < LineEnd[1]
But your above code changes the sign if LineStart[1] < LineEnd[1]
That's where I still have confussion. But in my VB6 code I get +15 before the checking code. So my checking code is like

Code: Select allIf LineStart(1) > LineEnd(1) OR LineStart(0) < LineEnd(0) Then
     ang = -ang
End If


I think I am wrong somewhere in the basic, but with in short span of time
I could understand a lot with my own experiments.
I hope with your great help I can be more clear.

2)
How come PS transform the Horizonatlly Fliped Shapes correctly?
Eg:
Shape B in Rot3.psd is H-Fliped and you know it was actally +15 and I expect it should return -15.
You said that our code can't deal the fliped shapes and return correct angle.
But if I give +15 in the Transform window the PS makes the shape to Un Rotated. That means PS knows the curent angle is -15 (what I expect) and it has to make it zero when user gives +15.
So there must be some trick to find the current visible angle, apart from the actual Path angle.

That's what I excpect. If we could hack that, then we could deal any shapes. More over while we do designing of Templates with shapes.
Sometime Fliping would save the time and makes well matched rotations.
(At least visually)

3)
Why don't I get my new Point (0) at LeftTop corner ?
My Steps:
I select shape layer content.
Add new ArtLayer
FIll or Stoke the selection
MakePath
Deselect

Now I expect my new Point(0) in my new layer at LeftTop corner. (If no bug with PS) Because I have not used any old Path or Points.
I made my own new Path now. Then why not PS should reset to default ?
Or where to do the hack to reset it ?


Thanks for your patience Mike.
soorya
Mike Hale

Problem with Negative Angle Rotation

Post by Mike Hale »

You could get all the pathpoints and determine which two are the top most points. Then of those two determine which is the left most point and use that point as the startline point and use the other as the endline point. It would be alot of work and I am not sure exactly how to do that.

But I really think that you would be better off by taking care when creating your templates.

Mike