Re: How to get tranformed bounds?
- From: Joergen Bech <jbech<NOSPAM>@<NOSPAM>post1.tele.dk>
- Date: Tue, 14 Nov 2006 21:26:26 +0100
On Tue, 14 Nov 2006 19:17:33 +0100, "Bob Powell [MVP]"
<bob@xxxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
You can transform the points corresponding to the corners and then get a
bounding rectangle for those using a simple min-max test.
To find the bounds of any arbitrary set of points:
int top=100000, left=100000, bottom=-100000,right=-100000;
foreach(Point p in points)
{
left=p.X<left ? p.X : left;
top=p.Y<top ? p.Y : top;
right=p.X>right ? p.X : right;
bottom=p.Y>bottom ? p.Y : bottom;
}
So what if all points are located at (1.000.000, 1000.000)?
Then
left = 1m < 100k ? 1m : 100k = 100k
i.e. left remains at 100k. Same fate befalls the top value.
A "collection" of a single point at (1.000.000, 1.000.000)
would result in a rectangle of
{X=100000,Y=100000,Width=900000,Height=900000}
rather than
{X=1000000,Y=1000000,Width=1,Height=1}
Look at the test code at the end. Correct me if I have made
a mistake.
Rectangle bounds=new Rectangle(left,top,right-left,bottom-top);
That is wrong as well: In this case, a "collection" of a single
point would result in a rectangle with a width and height of 0
rather than 1. Is that really the intention?
To correct both problems, I suggest the following approach:
1) Initialize left/top/right/bottom to the first point, then loop
through the rest of the points and *expand* the rectangle
like this:
left = Math.Min(p.X, left)
right = Math.Max(p.X, right)
top = Math.Min(p.Y, top)
bottom = Math.Max(p.Y, bottom)
2) Add 1 to the resulting width and height.
/Joergen Bech
---snip---
Private Sub TestNonWorkingBob()
Dim top As Integer = 100000
Dim left As Integer = 100000
Dim bottom As Integer = -100000
Dim right As Integer = -100000
Dim pts() As Point = New Point() {New Point(1000000, 1000000)}
For Each p As Point In pts
left = CType(IIf(p.X < left, p.X, left), Integer)
top = CType(IIf(p.Y < top, p.Y, top), Integer)
right = CType(IIf(p.X > right, p.X, right), Integer)
bottom = CType(IIf(p.Y > bottom, p.Y, bottom), Integer)
Next
Dim bounds As New Rectangle(left, top, right - left, bottom -
top)
Debug.WriteLine(bounds.ToString)
End Sub
---snip---
.
- References:
- How to get tranformed bounds?
- From: Fabio
- Re: How to get tranformed bounds?
- From: Bob Powell [MVP]
- How to get tranformed bounds?
- Prev by Date: Re: How to get tranformed bounds?
- Next by Date: GDI+ is it possible to print to file?
- Previous by thread: Re: How to get tranformed bounds?
- Next by thread: Re: How to get tranformed bounds?
- Index(es):
Relevant Pages
|