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 9079331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T19:48:44+00:00 2026-06-16T19:48:44+00:00

I have a square shape like this that is devided to 9 rectangles: So

  • 0

I have a square shape like this that is devided to 9 rectangles:

enter image description here

So I want to instead of making a long list of like this:

if(rectForward.Contains(touchPoint))
{
   return rectForward;
}
else if(rectForwardRight.Contains(touchPoint))
{
    return rectForwardRight;
}
//and so on!

Quickly check with a linq query to see which Rectangle contains the Point touchPoint.

Well I could use the time that I spend on this question to write the if/else stuff but I would like to see how it goes with linq

  • 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-06-16T19:48:45+00:00Added an answer on June 16, 2026 at 7:48 pm

    Try storing all your rectangles in a collection, then it becomes trivial to iterate over them:

    return myRectangles.FirstOrDefault(rect => rect.Contains(touchPoint))
    

    Depending on what you want to do if the point is not contained in any of them, you may want to use First only (which would throw an exception if the point isn’t contained by any of them); FirstOrDefault will (assuming you’re using the System.Windows.Rect class) return a default Rect (located at 0,0 with 0 size) so that may be difficult to check for.

    If you don’t want to store the rectangles in a collection, you can write a simple helper method to iterate over them in sequence and leverage that when you wish:

    private IEnumerable<Rect> GetSections()
    {
        yield return rectForward;
        yield return rectForwardRight;
        yield return rectRight;
        ...
    }
    

    To which your Linq call would use:

    return GetSections().FirstOrDefault(rect => rect.Contains(touchPoint))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a super type (Shape) and two children (like Square, Circle).
I have a sample EMF hierarchy like this - Parent : Shape Child 1
So I have a 2D array (named Data) that looks like: Shape 0 Shape
suppose my bitmap is like This image is actually in square transparent. I want
I have a 2 dimensional array forming a table: [color][number][shape ] ------------------------- [black][10 ][square
I have three images, and they are not square or rectangular in shape. They
I need to make a CheckBox to look like a ( shape instead of
Let's say I have an abstract parent class called shape, and that there are
I have a piece of js software that is structured like so: obj =
I have a QListView that displays square-shaped items. My problem is that when I

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.