-2

I need to generate a set of unique coordinates (A1, B1) and (A2, B2) with their value between the 0 and 1. This set of unique coordinates cannot lie between an existing set of coordinates ([x1], [y1]) and ([x2], [y2]) returned by a sql query. How can I write a script using C# that generates coordinates that are 1)NOT between the values returned by the query and 2)NOT equal to the values returned by the query?

The logic here is basically used to draw a set of boxes on a page. I need to draw new boxes with unique coordinates but these new boxes cannot overlap, or lie inside existing boxes. Any help is greatly appreciated!

4
  • 4
    Have you tried using >= and <=? Commented Aug 31, 2012 at 18:33
  • Are the boxes all the same size, or is that random also? Commented Aug 31, 2012 at 18:46
  • the size of the box is random as long as it doesn't overlap existing boxes Commented Aug 31, 2012 at 18:48
  • i figured this out using more SQL than C# Commented Sep 6, 2012 at 15:27

2 Answers 2

0

this works for about 99.99% or so ...

    void GenerateTest()
    {
        float x1, y1;
        float x2, y2;
         // fill x1,y1,x2,y2

        var r = new Random();
        Func<float> next = () => (float)r.NextDouble();

        var NotMatchingRect = RectangleF.FromLTRB(x1,y1,x2,y2);
        var Coordinates = Enumerable.Range(0, 1000).Select(i => new PointF(next(), next())).Where(p => !NotMatchingRect.Contains(p)).Distinct().Take(2).ToArray();
        if (Coordinates.Length != 2)
            throw new IndexOutOfRangeException();
    }
Sign up to request clarification or add additional context in comments.

1 Comment

how do I integrate my query: select distinct X1, Y1, X2, Y2 from table where instanceid=n and X1 !=0 and Y1 !=0 and X2 !=1 and Y2 !=1
0

Assuming the existence of a Random, named randomizer for convenience, you could do...

do
{
A1 = randomizer.NextDouble();
B1 = randomizer.NextDouble();
} while(!((A1>=X1&&A1<=X2&&B1>=Y1&&B1<=Y2)||(A1>=X2&&A1<=X1&&B1>=Y2&&B1<=Y1)) //not inside other rectangle

do
{
A2 = randomizer.NextDouble();
B2 = randomizer.NextDouble();
}
while(!((A1==A2&&B1==B2)||  //not equal to other random coordinate
(A2>=X1&&A2<=X2&&B2>=Y1&&B2<=Y2)||
(A2>=X2&&A2<=X1&&B2>=Y2&&B2<=Y1)|| //not inside other rectangle
(A1<=X1&&A2>=X2&&B1<=Y1&&B2>=Y2)||
(A1<=X2&&A2>=X1&&B1<=Y2&&B2>=Y1)||
(A2<=X1&&A1>=X2&&B2<=Y1&&B1>=Y2)||
(A2<=X2&&A1>=X1&&B2<=Y2&&B1>=Y1)||)) //not containing other rectangle

It's a bit(read: very very) cludgy (I'm kinda embarassed about it actually), but it should work, and generate all possible random rectangles... eventually.

2 Comments

I have something similar to this already, I guess the part I'm having trouble with is parameterizing the while statements and converting from SQL datareader results to C#. X1, Y1, X2, Y2 are all column names returned by a query. i.e. if X1={2, 4, 6} Y1={1, 3, 5} X2={3, 5, 8}, Y2={2, 4, 6}. My x1_rand and x2_rand cannot be between {2, 3}U{4, 5}U{6, 8} and my y1_rand and y2_rand cannot be between {1,2}U{3,4}U{5, 6}
Well, you could use any number of things. If you're already using SQLDataReader, the easiest way is probably to set up a dictionary. Here, this goes more into detail stackoverflow.com/a/1504989/1623719

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.