Re: How to get tranformed bounds?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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---



.



Relevant Pages

  • Re: VB Printer Dialog Problem (VB6)
    ... Now of course you will probably want the right and bottom margins to also be 15 millimeters from the edges of the physical page as well. ... One other point is that when you have calculated these values you need to check that those positions do not lie outside of the printer's current "printable rectangle". ... You can check this by checking the values returned by the GetDeviceCaps API for PHYSICALOFFSETX and PHYSICALOFFSETY, which are always in printer pixels so you'll need to convert. ...
    (comp.lang.basic.visual.misc)
  • Re: How to draw with a PEN ?? Please help
    ... Pamela - ... right sides of the rectangle. ... the bottom and right sides. ... I don't know whether that would offset all the additional polygon ...
    (microsoft.public.dotnet.framework.drawing)
  • Re: DrawText fails when attempting to retrieve string extents
    ... "Phil Taylor" wrote in message ... > DT_CALCRECT is filling Right and Bottom to make the rectangle big enough. ... list that will allow the specification of a Rect. ...
    (microsoft.public.win32.programmer.directx.graphics)
  • Re: A tables question!
    ... draw the AutoShape rectangle and set it behind text), ... of the Format AutoShape dialog is grayed out -- because the AutoShape ... bottom coincide with the table's top and bottom. ...
    (microsoft.public.word.pagelayout)
  • Re: add text to bottom of richtextbox, simple method? Solved
    ... It allows you to print the contents of a RichTextBox with selectable left, right, top and bottom page margins, and it works fine whether the RTB text runs to just one page or to several pages. ... Dim LineWidth As Long ... LeftMarginWidth As Long, ...
    (microsoft.public.vb.general.discussion)