3
$\begingroup$

I have two lines drawn with Mathematica, how do I calculate the angle between these two lines to have the numeric value of that angle? Thank you

$\endgroup$
  • $\begingroup$ I suggest you start by taking a look at ImageLines[] $\endgroup$ – dr.blochwave Oct 29 '15 at 7:56
  • $\begingroup$ I have only two lines that create an angle and I would extrapolate the angle in degrees, not the matrix. Just a number $\endgroup$ – Luca Samuele Capizzi Oct 29 '15 at 15:10
  • $\begingroup$ No I have only a image. No coordinates?? Two lines like a "V" and i would like to know the angle degrees value. 45°? 70°? 90°? 132°? Can you help me? $\endgroup$ – Luca Samuele Capizzi Oct 29 '15 at 15:23
  • $\begingroup$ I recommend a protractor. $\endgroup$ – bill s Oct 29 '15 at 15:47
2
$\begingroup$

I take it, you have drawn these lines yourself, did you? If so, you know their coordinates:

Line[{{x1,y1},{x2,y2}}]

and

  Line[{{X1,Y1},{X2,Y2}}]

Then using a standard expression of the vector analysis one finds the angle

v1 = {x2 - x1, y2 - y1}; v2 = {X2 - X1, Y2 - Y1}; ArcCos[v1.v2/(Sqrt[v1.v1]*Sqrt[v2.v2])] For example, if

 x1 = 0;
x2 = 0;
y1 = 1;
y2 = 2;
X1 = 0;
X2 = 1;
Y1 = 0;
Y2 = 1;

one finds

ArcCos[v1.v2/(Sqrt[v1.v1]*Sqrt[v2.v2])]

(* \[Pi]/4  *)

Have fun!

Later edit: to address your comment. In that case I have a function"metrics" to do such things. Please find below the description along with the code

Description of the function metrics[...]

The function metrics[...] is designed to measure coordinates of points of any image "image", distances between any two image points, radii of roundings and angles.

The argument "image" must have the head "Image", since it uses few functions specific for images. If the desired object has the head "Graphics" it should be wrapped by the function "Image".

Operating the function metrics[...]

By default all measurement tools are switched off. They may be independently switched on by checking the corresponding checkboxes.

The desired points are selected by locators. The latter have several appearances that are different for different tools.

The gauges on top of the screen control the sizes of the locators and the thicknesses of all lines.

The "Show right angle" checkbox switches on the right angle tool, which is the orientation, rather than the measurement tool.

The measured values can be read in the panels below the image.

Both the coordinates, the distance and radius are measured in pixels and then rescaled and returned in millimeters. The angle is returned both in degrees and in radians.

The current value of the coordinates, distance, radius and angle can be memorized in the global variables: copyCoordinates, copyDistance, copyRadius and copyAngle by pressing the corresponding buttons below the image. By evaluating the corresponding variables one gets the value in question in the notebook.

 metrics[image_] :=
  Manipulate[
   DynamicModule[{distance = 0, coordinates = {0, 0}, rad = 0, 
     angle1 = 0, angle2 = 0, x1 = ImageDimensions[image][[1]]/3, 
     y1 = ImageDimensions[image][[2]]/2., 
     x2 = 2*ImageDimensions[image][[1]]/3., 
     y2 = 2 ImageDimensions[image][[2]]/3,
     x3 = 3 ImageDimensions[image][[1]]/4., 
     y3 = 3 ImageDimensions[image][[2]]/4,
     x4 = 0.5*ImageDimensions[image][[1]], 
     y4 = 0.5*ImageDimensions[image][[2]],
     x5 = 
      0.4*ImageDimensions[image][[1]] + 
       0.05*ImageDimensions[image][[1]], 
     y5 = 0.4*ImageDimensions[image][[2]] + 
       0.05*ImageDimensions[image][[2]],
     x6 = 0.8*ImageDimensions[image][[1]], 
     y6 = 0.3*ImageDimensions[image][[2]],
     x7 = 0.3*ImageDimensions[image][[1]], 
     y7 = 0.7*ImageDimensions[image][[2]],
     x8 = 0.4*ImageDimensions[image][[1]], 
     y8 = 0.4*ImageDimensions[image][[2]],
     x9 = 0.5*ImageDimensions[image][[1]], 
     y9 = 0.4*ImageDimensions[image][[2]],
     x10 = 0.4*ImageDimensions[image][[1]], 
     y10 = 0.5*ImageDimensions[image][[2]],

     loc1 = 
      Graphics[{Blue, Opacity[If[showLine, 1, 0]], 
        AbsoluteThickness[thickness], Circle[{0, 0}, 1]}, 
       ImageSize -> radius1],
     loc2 = 
      Graphics[{Opacity[If[showTarget, 1, 0]], Thick, 
        Arrowheads[0.4], {Darker@Red, 
         Arrow[{{-0.02, 0}, {-0.002, 0}}]}, {Darker@Red, 
         Arrow[{{0.02, 0}, {0.002, 0}}]}, {Darker@Red, 
         Arrow[{{0, -0.02}, {0, -0.002}}]}}, ImageSize -> {30, 30}],
     loc3 = 
      Graphics[{Darker[Green], Opacity[If[showCircle, 1, 0]], 
        AbsoluteThickness[thickness], Circle[{0, 0}, 1]}, 
       ImageSize -> radius1],
     loc4 = 
      Graphics[{Purple, Opacity[If[showAngle, 1, 0]], 
        AbsoluteThickness[thickness], Circle[{0, 0}, 1]}, 
       ImageSize -> radius1],
     locCross3 = 
      Graphics[{Darker@Green, Opacity[If[showCircle, 1, 0]], 
        AbsoluteThickness[thickness], {Line[{{-0.01, 0.}, {0.01, 0}}],
          Line[{{0, -0.01}, {0, 0.01}}]}}, ImageSize -> radius1],
     locCross4 = 
      Graphics[{Purple, Opacity[If[showAngle, 1, 0]], 
        AbsoluteThickness[thickness], {Line[{{-0.01, 0.}, {0.01, 0}}],
          Line[{{0, -0.01}, {0, 0.01}}]}}, ImageSize -> radius1],
     loc5 = 
      Graphics[{Darker@Cyan, Opacity[If[showRightAngle, 1, 0]], 
        AbsoluteThickness[thickness], Circle[{0, 0}, 1]}, 
       ImageSize -> radius1]
     },


    distance = 
     Dynamic[Round[Sqrt[(x1 - x2)^2 + (y1 - y2)^2]*25.4/72, 0.01]];
    coordinates = Dynamic[Round[{x3, y3}*25.4/72, 0.01]];
    rad = 
     Dynamic[Round[Sqrt[(x4 - x5)^2 + (y4 - y5)^2]*25.4/72, 0.01]];
    angle1 = 
     Dynamic[Round[
       ArcCos[((x6 - x7)^2 + (y6 - y7)^2 + (x7 - x8)^2 + (y7 - 
              y8)^2 - (x8 - x6)^2 - (y8 - y6)^2)/(2*
            Sqrt[(x6 - x7)^2 + (y6 - y7)^2]*
            Sqrt[(x7 - x8)^2 + (y7 - y8)^2])] // Chop, 0.01]];
    angle2 = 
     Dynamic[Round[
       360./(2 \[Pi])*
         ArcCos[((x6 - x7)^2 + (y6 - y7)^2 + (x7 - x8)^2 + (y7 - 
               y8)^2 - (x8 - x6)^2 - (y8 - y6)^2)/(2*
             Sqrt[(x6 - x7)^2 + (y6 - y7)^2]*
             Sqrt[(x7 - x8)^2 + (y7 - y8)^2])] // Chop, 0.01]];

    Column[{
      LocatorPane[
       Dynamic[{{x1, y1}, {x2, y2}, {x3, y3}, {x4, y4}, {x5, y5}, {x6,
           y6}, {x7, y7}, {x8, y8}, {x9, y9}, {x10, y10}}],
       Dynamic@Show[{image,

          Graphics[{RGBColor[0, 0, 0.7], Dashed, 
            Opacity[If[showLine, 1, 0]], AbsoluteThickness[thickness],
             Dynamic[Line[{{x1, y1}, {x2, y2}}]]
                                                                 }],

          Graphics[{Darker[Green], Opacity[If[showCircle, 1, 0]], 
            AbsoluteThickness[thickness], 
            Circle[{x4, y4}, Sqrt[(x4 - x5)^2 + (y4 - y5)^2]]}],

          Graphics[{Purple, Opacity[If[showAngle, 1, 0]], 
            AbsoluteThickness[thickness], 
            Dynamic[Line[{{x6, y6}, {x7, y7}, {x8, y8}}]]}],

          Graphics[{Darker@Cyan, Opacity[If[showRightAngle, 1, 0]], 
            AbsoluteThickness[thickness], 
            Dynamic[Line[{{x9, y9}, {x10, y10}}]], 
            Dynamic[
             Rotate[Line[{{x9, y9}, {x10, y10}}], 
              90 Degree, {x10, y10}]]
                                                                 }]

                               },
         ImageSize -> ImageDimensions[image]], 
       Appearance -> {loc1, loc1, loc2, locCross3, loc3, loc4, 
         locCross4, loc4, loc5, loc5}
                             ],
      Row[{Panel[
         If[showLine, 
          Row[{Style["Distance:", 10, Blue], Spacer[10], 
            Style[distance, 10, Blue], Style[" mm", 10, Blue]}], 
          Style["Distance:  --", 10, Blue]], ImageSize -> {160, 50}, 
         Alignment -> Center],
        Panel[
         If[showTarget, 
          Row[{Style["Coordinate: ", 10, Darker@Red], 
            Style[coordinates, 10, Darker@Red], 
            Style[" mm", 10, Darker@Red]}], 
          Style["Coordinate: --", 10, Darker@Red]], 
         ImageSize -> {160, 50}, Alignment -> Center]}, 
       Alignment -> Center],
      Row[{Panel[
         Row[{Style["Radius: ", 10, Darker@Green], 
           If[showCircle, 
            Row[{Style[rad, 10, Darker@Green], 
              Style[" mm", 10, Darker@Green]}], 
            Style["--", 10, Darker[Green]]]}], ImageSize -> {160, 50},
          Alignment -> Center],
        Panel[
         Row[{Style["Angle: ", 10, Purple], 
           If[showAngle, 
            Row[{Style[angle1, 10, Purple], 
              Style[" rad=", 10, Purple], Style[angle2, 10, Purple], 
              Style["\[Degree]", 10, Purple]}], 
            Style["--", 10, Purple]]}], ImageSize -> {160, 50}, 
         Alignment -> Center]
        }, Alignment -> Center],
      Row[{Button[Style["Copy distance", 10, Blue], 
         Clear[copyDistance]; copyDistance = N[distance]],
        Button[Style["Copy coordinate", 10, Darker@Red], 
         Clear[copyCoordinates]; copyCoordinates = N[coordinates]], 
        Button[Style["Copy radius", 10, Darker[Green]], 
         Clear[copyRadius]; 
         copyRadius = 
          If[showCircle, N[rad], 
           "To get value the second argument should be True"]],
        Button[Style["Copy angle", 10, Purple], Clear[copyAngle]; 
         copyAngle = 
          If[showAngle, N[angle1], 
           "To get value the third argument should be True"]]
        }, Alignment -> Center]

      }, Alignment -> Center]
    ],

   Column[{
     Row[{Control[{{showLine, False, 
          Style["Distance measurement tool", 12]}, {True, False}}],
       Spacer[30], 
       Control[{{showTarget, False, 
          Style["Coordinate measurement tool", 12]}, {True, False}}]}],

     Row[{Control[{{showAngle, False, 
          Row[{Style["Angle measurement tool", 12], 
            Spacer[14]}]}, {True, False}}],
       Spacer[30], 
       Control[{{showCircle, False, 
          Row[{Style["Radius measurement tool", 12], 
            Spacer[24]}]}, {True, False}}]}, Alignment -> Left],
     Control[{{showRightAngle, False, 
        Row[{Style["Show right angle", 12], Spacer[57]}]}, {True, 
        False}}],

     Control[{{radius1, 10, 
        Row[{Style["Locator size", 12], Spacer[15]}]}, 3, 50, 
       HorizontalGauge[##, ImageSize -> {200, 40}] &}],

     Control[{{thickness, 0.1, Style["Lines thickness", 12]}, 0.1, 5, 
       HorizontalGauge[##, ImageSize -> {200, 40}] &}]
     }, Alignment -> Left],


   ControlType -> {Checkbox, Checkbox, Checkbox, Checkbox, Checkbox, 
     HorizontalGauge, HorizontalGauge, HorizontalGauge}, 
   ControlPlacement -> {Top, Top, Top, Top, Top, Top, Top, Top}, 
   SaveDefinitions -> True];

That is how it should look like. I applied it to an arbitrary experimantal image:

enter image description here

Have fun still!

$\endgroup$
  • $\begingroup$ There is also VectorAngle $\endgroup$ – Kuba Oct 29 '15 at 9:29
  • $\begingroup$ @Kuba Thank you, I did not know. Why do not you give it as an answer. It is better than mine. $\endgroup$ – Alexei Boulbitch Oct 29 '15 at 12:07
  • $\begingroup$ Feel free to include it, the main thing is still to notice that OP said "drawn" in MMA so he is probably aware of coordinates in some form. $\endgroup$ – Kuba Oct 29 '15 at 12:08
  • $\begingroup$ No I have only a image. Two lines like a "V" and i would like to know the angle degrees value. 45°? 70°? 90°? 132°? Can you help me? $\endgroup$ – Luca Samuele Capizzi Oct 29 '15 at 15:21
  • $\begingroup$ @Luca Samuele Capizzi See the edit $\endgroup$ – Alexei Boulbitch Oct 29 '15 at 15:42

Not the answer you're looking for? Browse other questions tagged or ask your own question.