Author Topic: Finding the min point in a collection of points  (Read 2773 times)

0 Members and 1 Guest are viewing this topic.

Proctor

  • Guest
Finding the min point in a collection of points
« on: November 21, 2011, 08:19:32 PM »
Hello: I was wondering if there was any method available using autocad .net api to find the min point in
a collection of points?

Thank you,
Proctor

Note: Using Autocad 2009

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8715
  • AKA Daniel
Re: Finding the min point in a collection of points
« Reply #1 on: November 21, 2011, 08:57:40 PM »
maybe something like.?.

Code: [Select]
           
            _AcDb.Extents3d extents = new _AcDb.Extents3d();
            foreach (_AcGe.Point3d point in _AcGe.Point3dCollection)
               extents.AddPoint(point);
            extents.MinPoint;

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Finding the min point in a collection of points
« Reply #2 on: November 21, 2011, 09:12:38 PM »
maybe something like.?.

Code: [Select]
           
            _AcDb.Extents3d extents = new _AcDb.Extents3d();
            foreach (_AcGe.Point3d point in _AcGe.Point3dCollection)
               extents.AddPoint(point);
            extents.MinPoint;

That sure beats converting the Collection to a List and running a Linq .Orderby on the list

:)
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Finding the min point in a collection of points
« Reply #3 on: November 22, 2011, 02:06:43 AM »
Hi

What does "min point" mean ?

In a collection containing 2 point : { (1, 0),  (0, 1) }, which is the "min point" ?
For this collection, Daniel's method should return (0, 0) as "min point" which does not belong to the collection.

I think you have to define more precisely the criteria used for "min point" and this criteria can be use with the IEnumerable .OrderBy() Linq extension method as Kerry said or the List<T>.Sort().

But as you don't have to sort the whole collection, the Linq Aggregate() extension method should be faster.

Example to get the "min point" comparing X coordinate first, then Y in case of equality, then Z.

Code: [Select]
        private Point3d MinPoint(IEnumerable<Point3d> pts)
        {
            return pts.Aggregate((p1, p2) =>
                {
                    if (p1.X == p2.X)
                    {
                        if (p1.Y == p2.Y)
                        {
                            if (p1.Z < p2.Z) return p1;
                            else return p2;
                        }
                        else if (p1.Y < p2.Y) return p1;
                        else return p2;
                    }
                    else if (p1.X < p2.X) return p1;
                    else return p2;
                });
        }

If you're not comfortable with lambda expressions, you can define a separated method for the accumulator

Exemple comaparing the coordinates sum first, then X then Y coordinate

Code: [Select]
        private Point3d ComparePoints(Point3d p1, Point3d p2)
        {
            double s1 = p1.X + p1.Y + p1.Z;
            double s2 = p2.X + p2.Y + p2.Z;
            if (s1 == s2)
            {
                if (p1.X == p2.X)
                {
                    if (p1.Y < p2.Y) return p1;
                    else return p2;
                }
                else if (p1.X < p2.X) return p1;
                else return p2;
            }
            else if (s1 < s2) return p1;
            else return p2;
        }
        ...
        Point3d minPt = pts.Aggregate(ComparePoints)
« Last Edit: November 22, 2011, 02:45:03 AM by gile »
Speaking English as a French Frog

Proctor

  • Guest
Re: Finding the min point in a collection of points
« Reply #4 on: November 22, 2011, 12:13:29 PM »
Thank you all for your replies. To better define what I mean when I want to find the min point....
I would like to find the point in the collection that has the minx and miny.

gile, do you think using private Point3d ComparePoints(Point3d p1, Point3d p2)
would work for this situation?

Thanks again,
Proctor

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Finding the min point in a collection of points
« Reply #5 on: November 22, 2011, 01:12:28 PM »
Quote
gile, do you think using private Point3d ComparePoints(Point3d p1, Point3d p2)
would work for this situation?

I cannot reply for you.
In most cases, yes. The coordinates sum will do the trick.
But you have to think about the cases this sum is equal for two or more points: (0, 1, 0) (1, 0, 0) (3, -2, 0) (-1, 2, 0)... which of these do you consider "the min point" ?
Actually, the ComparePoints method returns the lowest X coordinate (-1, 2, 0).
« Last Edit: November 22, 2011, 01:19:18 PM by gile »
Speaking English as a French Frog

LE3

  • Guest
Re: Finding the min point in a collection of points
« Reply #6 on: November 25, 2011, 10:43:00 AM »
here we use a lot linq...

so something like:
Quote
IEnumerable<Point3d> query = pts.Cast<Point3d>();
double x = query.Min(p => p.X);
Point3d pt = query.First(p => p.X == x);
To get the point with the min() X... and start exploring from there (tons of samples around the net about linq) - hth