Draw line with angle && Continue line with another angle(); .Net

Draw line with angle && Continue line with another angle(); .Net

Anonymous
Not applicable
2,628 Views
12 Replies
Message 1 of 13

Draw line with angle && Continue line with another angle(); .Net

Anonymous
Not applicable

I want to draw a line which starts from user insertion point with an angle (180 deg) of length x,

and wanted to draw another lines on both the end points vertical down with an angle (270 deg ) of length x on both sides

 

please help me whats gone wrong with my code 

 

Thanks in Advance ;; 

 

double angle = 180;
double length = 50;

 

double angle1 = 270;
double length1 = 10;


BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
PromptPointOptions ppo = new PromptPointOptions("Please Specify the Point");

PromptPointResult ppr = ed.GetPoint(ppo);

if (ppr.Status != PromptStatus.OK)
return;

Point3d startPt = ppr.Value;

Point3d endPt = new Point3d(startPt.X + Math.Cos(angle) * length, startPt.Y + Math.Sin(angle) * length, 0);

Line l1 = new Line(startPt, endPt);

btr.AppendEntity(l1);
tr.AddNewlyCreatedDBObject(l1, true);

 

//Secondline

 

Point3d endPt2 = new Point3d(endPt.X + Math.Cos(angle1) * length1, endPt.Y + Math.Sin(angle1) * length1, 0);

Line l2 = new Line(endPt, endPt2);

btr.AppendEntity(l2);

 

//Thirdline

 

Point3d endPt3 = new Point3d(endPt.X + Math.Cos(angle1) * length1 , endPt.Y + Math.Sin(angle1) * length1 , 0);

Line l3 = new Line(startPt, endPt3);

btr.AppendEntity(l3);


tr.Commit();

0 Likes
Accepted solutions (2)
2,629 Views
12 Replies
Replies (12)
Message 2 of 13

Juergen_Becker
Advocate
Advocate
Accepted solution

Hi,

 

in all the languages (Visual Lisp, .Net etc.) you have to use angle in radians (PI) not in decimal.

 

180° = 1PI

90 = 1/2 PI

 

please try that before other problems has to be resolved.

 

        public static double RadAngle(double doubleAngle)
        {
            return doubleAngle * System.Math.PI / 180;
        }
        public static double DecAngle(double RadAngle)
        {
            return RadAngle * 180 / System.Math.PI;
        }

Use above Code to convert from one to another.

 

Regards Jürgen

I hope my tip helps. If so then give me kudos and mark the tip as a solution.
Thanks.

Jürgen A. Becker
Building Services

Development and Support
Autodesk Forge Spezialist


CAD-Becker.de
https://www.CAD-Becker.de

0 Likes
Message 3 of 13

ActivistInvestor
Mentor
Mentor
Accepted solution

@Anonymous wrote:

I want to draw a line which starts from user insertion point with an angle (180 deg) of length x,

and wanted to draw another lines on both the end points vertical down with an angle (270 deg ) of length x on both sides

 

please help me whats gone wrong with my code 

 

 


Using the extension methods included below,

 

   double angle = 180;
   double length = 50;

   double angle1 = 270;
   double length1 = 10;

   Point3d startPt = ppr.Value;

   Point3d endPt = startPt.To(angle.ToRadians(), length);

   //Secondline

   Point3d endPt2 = startPt.To(angle1.ToRadians(), length1);

   //Thirdline

   Point3d endPt3 = endPt.To(angle1.ToRadians(), length1);

Extension methods for converting from angle/radians and computing polar coordinates:

 

using System;
using Autodesk.AutoCAD.Geometry;

namespace Autodesk.AutoCAD.Geometry
{
   public static class MyGeometryExtensions
   {
      /// <summary>
      /// Converts a double representing an angle in radians to degrees
      /// </summary>
      /// <param name="radians">The angle in radians</param>
      /// <returns>The angle in degrees</returns>

      public static double ToDegrees(this double radians)
      {
         return ((radians / 3.1415926535897931) * 180.0);
      }

      /// <summary>
      /// Converts a double representing an angle in degrees to radians
      /// </summary>
      /// <param name="degrees">The angle in degrees</param>
      /// <returns>The angle in radians</returns>

      public static double ToRadians(this double degrees)
      {
         return ((degrees / 180.0) * 3.1415926535897931);
      }

      /// <summary>
      /// 2D Polar coordinate specificiation (basepoint/angle/distance)
      /// </summary>
      /// <param name="basepoint">The basepoint from which the resulting point is computed</param>
      /// <param name="AngleInXYPlane">The direction (in radians) to the resulting point</param>
      /// <param name="distance">The distance to the resulting point</param>
      /// <returns>The point at the given distance and direction from the base point</returns>

      public static Point3d To(this Point3d basepoint, double angleInXYPlane, double distance)
      {
         return new Point3d(
            basepoint.X + (distance * Math.Cos(angleInXYPlane)),
            basepoint.Y + (distance * Math.Sin(angleInXYPlane)),
            basepoint.Z);
      }

   }

} 

 

0 Likes
Message 4 of 13

Juergen_Becker
Advocate
Advocate

Hi,

 

please use System.Math.PI instead of 3.1415926535897931.

 

Regards Jürgen

I hope my tip helps. If so then give me kudos and mark the tip as a solution.
Thanks.

Jürgen A. Becker
Building Services

Development and Support
Autodesk Forge Spezialist


CAD-Becker.de
https://www.CAD-Becker.de

0 Likes
Message 5 of 13

ActivistInvestor
Mentor
Mentor

@Juergen_Becker wrote:

Hi,

 

please use System.Math.PI instead of 3.1415926535897931.

 

Regards Jürgen


That's the value of Math.PI.

0 Likes
Message 6 of 13

Juergen_Becker
Advocate
Advocate

Hi,

 

I have to contradict that because the number pi does not end otherwise the result will be inaccurate.

 

That's important when big coordinates are used.
 
Regards Jürgen
I hope my tip helps. If so then give me kudos and mark the tip as a solution.
Thanks.

Jürgen A. Becker
Building Services

Development and Support
Autodesk Forge Spezialist


CAD-Becker.de
https://www.CAD-Becker.de

0 Likes
Message 7 of 13

_gile
Consultant
Consultant

Juergen_Becker a écrit :

Hi,

 

I have to contradict that because the number pi does not end otherwise the result will be inaccurate.

 

That's important when big coordinates are used.
 
Regards Jürgen

Did you just try something like this?

 

Console.WriteLine("{0:R}",Math.PI);

or like this:

 

Console.WriteLine(Math.PI == 3.1415926535897930);
Console.WriteLine(Math.PI == 3.1415926535897931);
Console.WriteLine(Math.PI == 3.1415926535897932);

You should learn a little about double-precision floating-point before asserting things about System.Math.PI accuracy.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 8 of 13

ActivistInvestor
Mentor
Mentor

@Juergen_Becker wrote:

Hi,

 

I have to contradict that because the number pi does not end otherwise the result will be inaccurate.

 

That's important when big coordinates are used.
 
Regards Jürgen

I think you're mistaken.  Math.PI is a const, not a function or a number of infinite resolution.

 

The compiler replaces references to a const with the value it is defined to, so the compiled code does not contain "Math.PI", it contains the value shown below, that PI is defined to by the System.Math class:

 

   // Portion of the System.Math class as rendered by ILSpy:

namespace System
{
public static class Math { private static double doubleRoundLimit = 1E+16; private const int maxRoundingDigits = 15; /// <summary>Represents the ratio of the circumference of a circle to its diameter, specified by the constant, PI.</summary>
[__DynamicallyInvokable] public const double PI = 3.1415926535897931; //// <<<remainder of Math class omitted>>> }
}

 

0 Likes
Message 9 of 13

Juergen_Becker
Advocate
Advocate

Hi,

 

I know what double is. You do not have to teach me.

Nevertheless, use Math.PI.
 
Regards Jürgen

 

I hope my tip helps. If so then give me kudos and mark the tip as a solution.
Thanks.

Jürgen A. Becker
Building Services

Development and Support
Autodesk Forge Spezialist


CAD-Becker.de
https://www.CAD-Becker.de

0 Likes
Message 10 of 13

ActivistInvestor
Mentor
Mentor

@Juergen_Becker wrote:

Hi,

 

I know what double is. You do not have to teach me.

Nevertheless, use Math.PI.
 
Regards Jürgen

 


You obviously do not know what a const is, because you mistakenly believe that the use of 'Math.PI' results in a more-precise value than the literal value that I used in the code I posted.

 

So, let me say this to you again one more time: Math.PI is a const defined by the Math class, and its defined value is 3.1415926535897931

 

We will use whatever we care to use, as long as it is correct.

 

0 Likes
Message 11 of 13

Juergen_Becker
Advocate
Advocate

Hi,

 

I also know what a const is.

What I want to say is that you better use System.Math.PI because 

 

1. You don't have to declare your own PI

2. You can't do any mistakes.

 

What about this?

        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            decimal m_PI = 0;
            m_PI = PI();

            Application.Run(new Form1());
        }

        static decimal PI()
        {
            return PI(1) * 2;
        }

        static decimal PI(int i)
        {
            if (i > 60)
            {
                return i;
            }
            else
            {
                return 1 + (i / (1 + (2.0m * i))) * PI(i + 1) ;
            }
        }

This Little Code return PI as a decimal with much more precision.

 

Regards Jürgen

I hope my tip helps. If so then give me kudos and mark the tip as a solution.
Thanks.

Jürgen A. Becker
Building Services

Development and Support
Autodesk Forge Spezialist


CAD-Becker.de
https://www.CAD-Becker.de

0 Likes
Message 12 of 13

_gile
Consultant
Consultant

Juergen_Becker a écrit :

What about this?

        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            decimal m_PI = 0;
            m_PI = PI();

            Application.Run(new Form1());
        }

        static decimal PI()
        {
            return PI(1) * 2;
        }

        static decimal PI(int i)
        {
            if (i > 60)
            {
                return i;
            }
            else
            {
                return 1 + (i / (1 + (2.0m * i))) * PI(i + 1) ;
            }
        }

This Little Code return PI as a decimal with much more precision.


So what?

 

Even implemented in a more usable way (as the following code examples), to be used with AutoCAD (which only uses the Double type for floating numbers), MathExtension.PI will have to be converted into a double, and (double)MathExtension.PI returns: 3.1415926535897931, which is the same as System.Math.PI value...

 

 

 

 

    public class MathExtension
    {
        static MathExtension()
        {
            PI = 2 * ComputePI(1);
        }

        public static decimal PI { get; }

        static decimal ComputePI(int i)
        {
            if (i > 60)
                return i;
            else
                return 1 + (i / (1 + (2.0m * i))) * ComputePI(i + 1);
        }
    }

 

or, equivalent, but much more simple and direct (as you know what a const is):

 

 

    public static class MathExtension
    {
        public const decimal PI = 3.141592653589793250103076431m;
    }

 

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 13 of 13

ActivistInvestor
Mentor
Mentor

 

You do not know what a const is, as you made that perfectly clear in your first reply, by suggesting that Math.PI is more accurate than the literal const value in my code, which is the value that Math.PI is defined as in System.Math. You are completely mistaken about that, so don't try to change the story now.

 

You also do not understand that a System.Double's precision is limited to 15 places to the right of the decimal point.

 

It does not matter what the resolution of a System.Decimal is, because computations are always done using System.Double, not System.Decimal, and all APIs that accept radian angle arguments declare tham as System.Double, not System.Decimal.

 

Here is another portion of the disassembled code of System.Math, do you see any parameters declared as decimal ?

 

 


public static extern double Acos(double d); public static extern double Asin(double d); public static extern double Atan(double d); public static extern double Atan2(double y, double x);

 


@Juergen_Becker wrote:

Hi,

 

I also know what a const is.

What I want to say is that you better use System.Math.PI because 

 

1. You don't have to declare your own PI

2. You can't do any mistakes.

 

What about this?

        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            decimal m_PI = 0;
            m_PI = PI();

            Application.Run(new Form1());
        }

        static decimal PI()
        {
            return PI(1) * 2;
        }

        static decimal PI(int i)
        {
            if (i > 60)
            {
                return i;
            }
            else
            {
                return 1 + (i / (1 + (2.0m * i))) * PI(i + 1) ;
            }
        }

This Little Code return PI as a decimal with much more precision.

 

Regards Jürgen


 

0 Likes