Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 700699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:31:39+00:00 2026-05-14T03:31:39+00:00

I have the following code: foreach (Tuple<Point, Point> pair in pointsCollection) { var points

  • 0

I have the following code:

foreach (Tuple<Point, Point> pair in pointsCollection)
        {
            var points = new List<Point>()
                        {
                            pair.Value1,
                            pair.Value2
                        };

        }

Within this foreach, I would like to be able to determine which pair of points has the most significant length between the coordinates for each point within the pair.

So, let’s say that points are made up of the following pairs:

(1) var points = new List<Point>()
{
new Point(0,100),
new Point(100,100)
};

(2) var points = new List<Point>()
{
new Point(150,100),
new Point(200,100)
};

So I have two sets of pairs, mentioned above. They both will plot a horizontal line. I am interested in knowing what the best approach would be to find the pair of points that have the greatest distance between, them, whether it is vertically or horizontally. In the two examples above, the first pair of points has a difference of 100 between the X coordinate, so that would be the point with the most significant difference. But if I have a collection of pairs of points, where some points will plot a vertical line, some points will plot a horizontal line, what would be the best approach for retrieving the pair from the set of points whose difference, again vertically or horizontally, is the greatest among all of the points in the collection?

Thanks!

Chris

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T03:31:39+00:00Added an answer on May 14, 2026 at 3:31 am

    Use OrderBy to create an ordering based on your criteria, then select the first one. In this case order by the maximum absolute difference between the horizontal and vertical components in descending order.

    EDIT: Actually, I think you should be doing this on the Tuples themselves, right? I’ll work on adapting the example to that.

    First, let’s add an extension for Tuple<Point,Point> to calculate it’s length.

     public static class TupleExtensions
     {
          public static double Length( this Tuple<Point,Point> tuple )
          {
              var first = tuple.Item1;
              var second = tuple.Item2;
              double deltaX = first.X - second.X;
              double deltaY = first.y - second.Y;
              return Math.Sqrt( deltaX * deltaX + deltaY * deltaY );
          }
    }
    

    Now we can order the tuples by their length

    var max = pointCollection.OrderByDescending( t => t.Length() )
                             .FirstOrDefault();
    

    Note: it is faster to just iterate over the collection and keep track of the maximum rather than sorting/selecting with LINQ.

    Tuple<Point,Point> max = null;
    foreach (var tuple in pointCollection)
    {
         if (max == null || tuple.Length() > max.Length())
         {
             max = tuple;
         }
    }
    

    Obviously, this could be refactored to an IEnumerable extension if you used it in more than one place.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: List<HtmlMeta> metas = new List<HtmlMeta>(); foreach (Control c in
I have the following code: foreach (var control in this.Controls) { } I want
I have the following code: Parallel.ForEach(this.listView2.CheckedItems, new ParallelOptions { MaxDegreeOfParallelism = 4 }, (CheckedItem)
We have the following cycle in the code: foreach (var event in events) {
I have the following code segment: var framelist = BL.FooBL.GetFrame() ; var foreach( var
I have the following code foreach (var rssItem in rss.Channel.Items) { // ... }
i have the following code: foreach (var str in usedCSS) { if (CSS.Contains(str)) Response.Write(str);
I have the following code : foreach( ... ) { $m = new Memcache;
I have the following code <ul id =menu> <%foreach (var pckg in Model) {
I have following code snippet in c# List<int> list = new List<int>() { 1,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.