get straight line rotation angle in Google apps script

Viewed 121

Trying to get rotation angle of straight line with getRotation() method but it always returning 0.0

Tried this method: https://developers.google.com/apps-script/reference/slides/line#getrotation

Getting output: on line move from top or bottom always getting rotation 0.0.

function lineRotation() {
  var selection = SlidesApp.getActivePresentation().getSelection();
  if(selection.getPageElementRange() !== null){

   var pageElements = selection.getPageElementRange().getPageElements()

   // Iterate each page elements
   pageElements.forEach(function(item, index) {
    if(pageElements[index].getPageElementType() == 'LINE'){
    
      var rotation = pageElements[index].asLine().getRotation();
  
 }else{
   SlidesApp.getUi().alert('Please select line.');
 }
});

  }else{
   SlidesApp.getUi().alert('Please select elements.');
  }
 }

Desired output required: Line rotation in angle either it moved from top or bottom.

Update: Future scope - I want to get rotation and set rotation of line. If Line inclination < 45° ==> rotate horizontally or Line inclination > 45° ==> rotate vertically. https://prnt.sc/uervkw (line screenshot)

1 Answers

Rotation != angle of the line:

Check Line rotation:

Like other page elements, a line's rotation isn't the vertical angle of the line, but the rotation of its bounding box. When you create a line with specified start and end points, its rotation is always 0°.

If you want to get the actual rotation of a line, based on its start and end points, you have to get the position of these points.

You can get these positions via getStart() and getEnd(). Both these two methods return a Point, whose coordinates can be accessed via getX() and getY().

Based on these coordinates, you can calculate the angle between the line and the axis you desire using some basic trigonometry.

Related