1. Login to Give a bone

## Math in .Net

Im trying to port over some code and Im still fairly new to .net and cant readily find functions when needed.
How would I translate this code to vb.net
Code:
```(or
(and (= (car P2) (car P1)) (< (cadr P1) (cadr P2))) ;UP
(and (< (car P1) (car P2)) (< (cadr P1) (cadr P2))) ;UP-Right
(and (< (car P1) (car P2)) (= (cadr P2) (cadr P1))) ;Right
(and (< (car P1) (car P2)) (> (cadr P1) (cadr P2))) ;Right-Down
)
(setq P1 (polar P1 (angle P1 P2) 0.0625))```
Thanks,
matt

2. Login to Give a bone

## Re: Math in .Net

Its untested but it is something like this.

Code:
```Imports Autodesk.AutoCAD.Geometry

Public Class Class1

Private Function Polar5Net(ByVal objPoint1 As Point2d, ByVal objPoint2 As Point2d)
On Error Goto OnError
If (objPoint1.X = objPoint2.X And objPoint1.Y < objPoint2.Y) Or _
(objPoint1.X < objPoint2.X And objPoint1.Y < objPoint2.Y) Or _
(objPoint1.X < objPoint2.X And objPoint1.Y = objPoint2.Y) Or _
(objPoint1.X < objPoint2.X And objPoint1.Y > objPoint2.Y) Then
With objPoint1.GetVectorTo(objPoint2)
End With
End If
OnError:   Return Nothing
End Function

<LispFunction("Polar5")> _
Public Function Polar5LISP(ByVal rbfLISPArguments As ResultBuffer)
On Error GoTo OnError
Dim arrLISPArguments As TypedValue() = rbfLISPArguments.AsArray
If arrLISPArguments.Length = 2 Then
Dim objPoint1 As Point2d = CType(arrLISPArguments(0).Value, Point2d)
Dim objPoint2 As Point2d = CType(arrLISPArguments(1).Value, Point2d)
Return Polar5Net(objPoint1, objPoint2)
End If
OnError: Return New TypedValue(LispDataType.Nil, -1)
End Function

End Class```
I made a few modifications and exposed the function to LISP too.

3. Login to Give a bone

## Re: Math in .Net

Code:
```using Autodesk.AutoCAD.Geometry;

using System;

{
public class Sample
{
object Foo(Point2d pt1, Point2d pt2, double length) //PolarIf
{
object ret = null;

if (pt1.X == pt2.X && pt1.Y < pt2.Y | // up
pt1.X < pt2.X && pt1.Y < pt2.Y | // up-right
pt1.X < pt2.X && pt1.Y == pt2.Y | // right
pt1.X < pt2.X && pt1.Y > pt2.Y // right-down
)
{
double ang = pt1.GetVectorTo(pt2).Angle;

ret = new Point2d(pt1.X + length * Math.Cos(ang),
pt1.Y + length * Math.Sin(ang));
}

return ret;
}
}
}```
* Note - I typically reserve Try, Catch, Finally blocks for main code, unless the Method being called is unable to preclude an exception through code logic.

4. Login to Give a bone

## Re: Math in .Net

Thank you guys, I was in the middle of writing a GetVectorTo function. I was hoping Autodesk didnt leave out the little stuff.

I appreciate the examples,
matt

5. Login to Give a bone

## Re: Math in .Net

Originally Posted by matt.worland
Thank you guys, I was in the middle of writing a GetVectorTo function. I was hoping Autodesk didnt leave out the little stuff.

I appreciate the examples
You're welcome; I'm happy to help.

Along the lines of 'examples', one could also implement a simple Extension Method, which would reduce my code above, to this:

Code:
```using Autodesk.AutoCAD.Geometry;

{
public class Sample
{
static object Foo(Point2d pt1, Point2d pt2, double length)
{
return pt1.PolarIf(pt2, length);
}
}
}```
... And here's the dependent Extension Method:

Code:
```using System;

{
public static class Point2dExtensions
{
public static object PolarIf(this Point2d pt1, Point2d pt2, double length)
{
object ret = null;

if (pt1.X == pt2.X && pt1.Y < pt2.Y | // up
pt1.X < pt2.X && pt1.Y < pt2.Y | // up-right
pt1.X < pt2.X && pt1.Y == pt2.Y | // right
pt1.X < pt2.X && pt1.Y > pt2.Y // right-down
)
{
double ang = pt1.GetVectorTo(pt2).Angle;

ret = new Point2d(pt1.X + length * Math.Cos(ang),
pt1.Y + length * Math.Sin(ang));
}

return ret;
}
}
}```
Cheers

6. Login to Give a bone

## Re: Math in .Net

Originally Posted by matt.worland
... I was in the middle of writing a GetVectorTo function. I was hoping Autodesk didnt leave out the little stuff.
Also worthy of note; Object Browser is your friend.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•