0

I am using this code to create a rectangle:

public static Rectangle RectangleFromPoints(Point a, Point b)
    {
        return new Rectangle(Math.Min(a.X, b.X),
                             Math.Min(a.Y, b.Y),
                             Math.Abs(a.X - b.X),
                             Math.Abs(a.Y - b.Y));
    }

My issue is when I draw a horizontal or vertical line and then try to move it a.Y because a long negative number and throws an Overflow exception error.

EDIT

 public override void Translate(Size s)
    {
        var oldBounds = this.ExpandedBounds;
        _startLocator.Translate(s);
        _endLocator.Translate(s);
        Notify();
        _canvas.Repaint(System.Drawing.Rectangle.Union(oldBounds, this.ExpandedBounds));

This is the code for moving the line. And this is ExpandedBounds:

 public Rectangle ExpandedBounds
    {
        get
        {
            var rect = this.Bounds;
            rect.Inflate((LocatorHandle.WIDTH / 2) + 1, (LocatorHandle.HEIGHT / 2) + 1);
            return rect;
        }
    }
  • Can you show the code that producing the exception? – Rufus L Apr 06 '17 at 17:09
  • @RufusL That is the code producing the exception. I am fuzzy on where the number is being gotten though, I am using SmallDraw in C# if that is any help though. – Ludos The Brave Apr 06 '17 at 17:16
  • I don't think this will solve your problem, but I think you need to add 1 to the return values from `Math.Abs`. If you have a rectangle with corners at pixels (0, 0) and (5, 5), the height and width of that rectangle are 6, not 5. – Tanner Swett Apr 06 '17 at 17:19
  • @TannerSwett I am willing to give it a try! **EDIT** I believe it works, more testing is needed to make sure it didn't upset the rest of my code, but yeah, no more errors! – Ludos The Brave Apr 06 '17 at 17:20
  • The code you show above does not do anything that would cause an exception. I copied it and used it like this and it ran fine: `var rect = RectangleFromPoints(new Point(10, 20), new Point(-10, -20));` – Rufus L Apr 06 '17 at 17:27
  • @TannerSwett I don't get it...are you saying a "rectangle" with corners at (0,0) and (0,0) has a width of `1`? – Rufus L Apr 06 '17 at 17:31
  • @ShatteredSpades - Please post the code that demonstrates this statement: "when I draw a horizontal or vertical line and then try to move it" – Rufus L Apr 06 '17 at 17:32
  • @RufusL hmm, I will hunt around more to see if I can get anymore information on what was making it do that then. – Ludos The Brave Apr 06 '17 at 17:33
  • This question is totally unclear. – TaW Apr 06 '17 at 18:21
  • @TaW What can I do to make it clearer? – Ludos The Brave Apr 06 '17 at 18:30
  • For starters: What are you targetting: Winforms, WPF, ASP..? __Always__ tag your question correctly! - Next expand and maybe correct this sentence: _My issue is when I draw a horizontal or vertical line and then try to move it a.Y because a long negative number and throws an Overflow exception error._ do you mean you 'draw' a rectangle that is actually a line??? if so: Do you want to allow it or not?.. – TaW Apr 06 '17 at 18:40
  • @Rufus Yes, I am saying a rectangle (on a pixel grid) with corners at (0,0) and (0,0) has a width of 1. Such a rectangle is a single pixel, so it's 1 pixel tall and 1 pixel wide. (This isn't the case on the Euclidean plane, where there are no pixels. There, it's a degenerate rectangle with a height and width of 0.) – Tanner Swett Apr 06 '17 at 21:02

0 Answers0