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

  • Home
  • SEARCH
  • 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 6182969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:17:12+00:00 2026-05-24T01:17:12+00:00

I am adding rectangles on a Canvas using Random because I don’t know the

  • 0

I am adding rectangles on a Canvas using Random because I don’t know the width and height.

Code

  HProDataContext db = new HProDataContext();

        var RoomX = (from d in db.rooms select d.sizex).ToList();
        var RoomY = (from d in db.rooms select d.sizey).ToList();
        var random = new Random();
        for (int i = 0; i < RoomX.Count; i++)
        {

            RoomX[i] = (Convert.ToDouble(RoomX[i]) * 10).ToString();
            RoomY[i] = (Convert.ToDouble(RoomY[i]) * 10).ToString();

            var rectangle = new Rectangle()
            {
                Stroke = Brushes.Black,
                Fill = Brushes.SkyBlue,
                Width = Convert.ToDouble(RoomX[i]),
                Height = Convert.ToDouble(RoomY[i]),
                Margin = new Thickness(
                    left: random.NextDouble() * 300,
                    top: random.NextDouble() * 150,
                    right: 0,
                    bottom: 0),
                HorizontalAlignment = HorizontalAlignment.Left,
                VerticalAlignment = VerticalAlignment.Top
            };
            mainCanvas.Children.Add(rectangle);
        }
    }

It works good, but some rectangles are drawing over another rectangles.
How to draw rectangles there where is empty space?

enter image description here

  • 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-24T01:17:13+00:00Added an answer on May 24, 2026 at 1:17 am

    You can calculate a Rect for each WPF Rectangle and use IntersectsWith method found in Rect to test if the code can a draw a new Rectangle without overlaps. I also recommend using rectangle properties Canvas.Left and Canvas.Top instead of adjusting the Margin.

    I’ve included a simple example that you test to get you started.

    Here is some XAML containing 4 Rectangles:

    <Window x:Class="OverlapRectangeTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
      <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="60"/>
        </Grid.RowDefinitions>
        <Canvas x:Name="c" Grid.Row="0">
            <Rectangle x:Name="b1" Fill="Aqua" Canvas.Left="0" Canvas.Top="0" Width="100" Height="200"/>
            <Rectangle x:Name="b2" Fill="Bisque" Canvas.Left="0" Canvas.Top="0" Width="200" Height="100"/>
            <Rectangle x:Name="b3" Fill="BlueViolet"  Canvas.Left="250" Canvas.Top="0" Width="100" Height="200"/>
            <Rectangle x:Name="b4" Fill="Cornsilk"  Canvas.Left="111" Canvas.Top="0" Width="100" Height="200"/>
        </Canvas>
        <Button Grid.Row="1" Click="Button_Click"/>
      </Grid>
    </Window>
    

    Here is a button click event handler where I test the IntersectsWith method:

    private void Button_Click(object sender, RoutedEventArgs e)
    {
      double top1 = (double)b1.GetValue(Canvas.TopProperty);
      double left1 = (double)b1.GetValue(Canvas.LeftProperty);
      Rect rect1 = new Rect(left1, top1, b1.Width, b1.Height);
    
      double top2 = (double)b2.GetValue(Canvas.TopProperty);
      double left2 = (double)b2.GetValue(Canvas.LeftProperty);
      Rect rect2 = new Rect(left2, top2, b2.Width, b2.Height);
    
      double top3 = (double)b3.GetValue(Canvas.TopProperty);
      double left3 = (double)b3.GetValue(Canvas.LeftProperty);
      Rect rect3 = new Rect(left3, top3, b3.Width, b3.Height);
    
      double top4 = (double)b4.GetValue(Canvas.TopProperty);
      double left4 = (double)b4.GetValue(Canvas.LeftProperty);
      Rect rect4 = new Rect(left4, top4, b4.Width, b4.Height);
    
      bool rc0 = rect1.IntersectsWith(rect4);
      bool rc1 = rect1.IntersectsWith(rect2);
      bool rc2 = rect2.IntersectsWith(rect3);
      bool rc3 = rect3.IntersectsWith(rect1);
      bool rc4 = rect2.IntersectsWith(rect4);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have added canvas in the xaml file. From code iam creating two rectangles
When adding new properties to classes, I find myself typing the same things over
Adding contents to listview is a simple proceess like ListViewItem item = new ListViewItem();
I created a UserControl to display a popup using a TextBlock within a Canvas
I implemented adding ports as children to a rectangle Figure/EditPart/Model using the Logic example
I am programatically creating an ImageMap using VB.NET and then adding PolygonHotSpot s to
The code below does not adding rectangle. Can anyone advice What i missed ?
I'm adding mouse rotation to my 2D drawing program. I've got all the code
I'm writing a simple 2D game engine using the HTML5 canvas. I've come to
I'm adding rectangles to a wrap panel like this: For i = 0 to

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.