.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Convert list of 3d points to 2d points?

3 REPLIES 3
Reply
Message 1 of 4
rebar0
5072 Views, 3 Replies

Convert list of 3d points to 2d points?

Hi. I am using this to convert a list of 3d points to 2d points (z=0.0), and was wondering if there was a simpler way:

{code}
List<Point3d> my3dPoints = new List<Point3d>();
// added points to above
List<Point2d> my2dPoints = new List<Point2d>();
foreach( Point3d pnt in my3dPoints )
my2dPoints.Add( pnt.Convert2d( new Plane() ) );
{code}

Thank you.


{code}
3 REPLIES 3
Message 2 of 4
Anonymous
in reply to: rebar0

This may not come out right because the Autodesk discussion group software mangles code (isn't that just wonderful?).

{code}

List pnt3dList = new List();

// add items to pnt3dList

List pnt2dList = pnt3dList.ConvertAll( p => new Point2d( p.X, p.Y ) );

{code}

For a simple conversion (e.g, Z = 0.0), don't use Convert2d()
because it transforms the input point into the plane argument.
It's more efficient to just return a new Point2d.

Also, if your list of points is logically a set (e.g., no two elements
are coincident) and not order-dependent, you should probably
use a HashSet rather than a List.

In that case, you can use the Linq Select() method just like the
ConvertAll() method is used on the List example above:

{code}

HashSet points3d = new HashSet();

HashSet points2d = new HashSet(
points3d.Select( p => new Point2d( p.X, p.Y ) ) );

{code}


--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD
Supporting AutoCAD 2000 through 2010

http://www.acadxtabs.com

Email: string.Format("{0}@{1}.com", "tonyt", "caddzone");

wrote in message news:6309926@discussion.autodesk.com...
Hi. I am using this to convert a list of 3d points to 2d points (z=0.0), and was wondering if there was a simpler way:

{code}
List my3dPoints = new List();
// added points to above
List my2dPoints = new List();
foreach( Point3d pnt in my3dPoints )
my2dPoints.Add( pnt.Convert2d( new Plane() ) );
{code}

Thank you.


{code}
Message 3 of 4
Anonymous
in reply to: rebar0


I see that Tony has answered you,

but you may also want to have a look at this
discussion.


Topic: lambda expression to convert
lists  

 

 

Regards

Kerry Brown

 

Hi.
I am using this to convert a list of 3d points to 2d points (z=0.0), and was
wondering if there was a simpler way:

{code}
List<Point3d>
my3dPoints = new List<Point3d>();
// added points to
above
List<Point2d> my2dPoints = new List<Point2d>();
foreach(
Point3d pnt in my3dPoints )
my2dPoints.Add( pnt.Convert2d( new Plane() )
);
{code}

Thank you.


{code}
Message 4 of 4
Anonymous
in reply to: rebar0

Ouch!!!!!

If one needs to convert a list of TInput to a list of TOutput, why would they use ForEach() rather than ConvertAll() ?

If you look at the implementation of ConvertAll() with Reflector, you might note that it sets the initial capacity of the result
list to the size of the input list, which is something one should always do when the number of elements they're working with is
known, to avoid the need for the list to incrementally reallocate its internal array each time it must grow in capacity.

For another thing, using ForEach() that way also breaks a fundamental rule of FP, that would effectively prevent it from being
easily parallelized.

BTW, ConvertAll() can thought of as a functional equivalent to AutoLISP's (mapcar) when used with only a single list argument.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD
Supporting AutoCAD 2000 through 2010

http://www.acadxtabs.com

Email: string.Format("{0}@{1}.com", "tonyt", "caddzone");

"Kerry Brown" wrote in message news:6309941@discussion.autodesk.com...
I see that Tony has answered you,
but you may also want to have a look at this discussion.
http://www.theswamp.org/index.php?topic=31404.0
Topic: lambda expression to convert lists


Regards
Kerry Brown

"rebar0" wrote in message news:6309926@discussion.autodesk.com...
Hi. I am using this to convert a list of 3d points to 2d points (z=0.0), and was wondering if there was a simpler way:

{code}
List my3dPoints = new List();
// added points to above
List my2dPoints = new List();
foreach( Point3d pnt in my3dPoints )
my2dPoints.Add( pnt.Convert2d( new Plane() ) );
{code}

Thank you.


{code}

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost