[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.drawing

problems with flips/rotations of certain shapes

(sean)

1/7/2005 10:47:00 PM

I'm trying to debug some code and now I'm lost. Here's the problem.

I have a graphic editor with which I can draw shapes and other
graphical items. I can also do rotations (90 degree increments,
clockwise and counterclockwise) and flips (horizontal and vertical).
With some shapes, such as a curve, they 'walk' across the screen as
they are being rotated or flipped. Rotation and flips are being
accomplished with a matrix. There is no apparent pattern to the
'walking'. Sometimes they move only 1 in the x and y directions.
Sometimes more depending on how I've tried to compensate. I just can't
seem to put a definitive finger on where this is happening.

As far as I can tell, the calculation for determining the shapes
bounding rectangle is off a little. I'm not sure why this occurring.
I'm hoping that someone has had experience with this type of problem.

If my explanation is lacking, please let me know and I'll do my best to
explain further. I'm not sure if I can supply sample code but I might
be able to come up with a snippet if need be.

Thanks in advance

Sean

7 Answers

Bob Powell

1/8/2005 1:15:00 AM

0

Always draw as if you were drawing to an untransformed graphics device. The
matrix you use should always be fresh. That is to say that if you're drawing
is rotated 90 degrees and flipped horizontally then do hat every time you
draw. Dont rotate 90 degrees in one cycle and then flip horizontally in the
next.

Matrices can be compounded by multiplying them together. The order in which
you multiply is important.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tips...

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/f...

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.





<sean.slavin@wonderware.com> wrote in message
news:1105138006.490959.142200@f14g2000cwb.googlegroups.com...
> I'm trying to debug some code and now I'm lost. Here's the problem.
>
> I have a graphic editor with which I can draw shapes and other
> graphical items. I can also do rotations (90 degree increments,
> clockwise and counterclockwise) and flips (horizontal and vertical).
> With some shapes, such as a curve, they 'walk' across the screen as
> they are being rotated or flipped. Rotation and flips are being
> accomplished with a matrix. There is no apparent pattern to the
> 'walking'. Sometimes they move only 1 in the x and y directions.
> Sometimes more depending on how I've tried to compensate. I just can't
> seem to put a definitive finger on where this is happening.
>
> As far as I can tell, the calculation for determining the shapes
> bounding rectangle is off a little. I'm not sure why this occurring.
> I'm hoping that someone has had experience with this type of problem.
>
> If my explanation is lacking, please let me know and I'll do my best to
> explain further. I'm not sure if I can supply sample code but I might
> be able to come up with a snippet if need be.
>
> Thanks in advance
>
> Sean
>


(sean)

1/10/2005 5:32:00 PM

0

Bob

Thank you for your reply.

It looks as though I was mistaken in my original post. The code for a
curve is not using matrices. It is simply drawing and then getting the
bounds based on a set (3) of points. The error appears to be coming
from the bounds calculation.

Would that change anything?

Thanks

Sean

(sean)

1/11/2005 3:32:00 PM

0

Here is a sample that illustrates the error in the bounds which I
believe is then causing the error in flips and rotations.

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

public class DrawingSampleError : Form
{
Point[] m_controlPoints;
Point[] m_controlPoints2;
Rectangle m_cachedBounds;
float m_tension = 1.0F;

public static void Main()
{
Application.Run(new DrawingSampleError());
}

public DrawingSampleError()
{
Text = "Drawing Sample Error";

m_controlPoints = new Point[] { new Point(149, 199), new Point(199,
99), new Point(249, 199) };
m_controlPoints2 = new Point[] { new Point(29, 49), new Point(129,
149), new Point(29, 149) };
}

protected override void OnPaint(PaintEventArgs pea)
{
Graphics grfx = pea.Graphics;

GetBounds(m_controlPoints);

grfx.DrawRectangle(new Pen(Color.Red), m_cachedBounds);
grfx.DrawCurve(new Pen(ForeColor), m_controlPoints);

GetBounds(m_controlPoints2);

grfx.DrawCurve(new Pen(Color.Blue), m_controlPoints2);
grfx.DrawRectangle(new Pen(Color.Red), m_cachedBounds);
}

protected Rectangle GetBounds(Point[] controlPoints)
{
using (GraphicsPath graphicsPath = new GraphicsPath())
{
graphicsPath.AddCurve(controlPoints, m_tension);

RectangleF boundsRectF = RectangleF.Empty;
try
{
using (Pen tempPen = new Pen(Color.Black, 1))
{
graphicsPath.Widen(tempPen);
}

boundsRectF = graphicsPath.GetBounds();
}
catch
{
}

m_cachedBounds = new Rectangle(
(int)Math.Ceiling(boundsRectF.Left),
(int)Math.Round(boundsRectF.Top) + 1,
(int)Math.Round(boundsRectF.Width),
(int)Math.Round(boundsRectF.Height));
}

return m_cachedBounds;
}
}

Thanks

sean

(sean)

1/11/2005 10:32:00 PM

0

I think I figured this out. For those that are curious, it looks as
though the curve tension affects the bounding rectangle. Higher
tensions result in rectangles being drawn(calculated) beyond the curve.
Tensions close to 0.0 are the opposite. The default tension of .5
appears to calculate the bounding rectangles for the curves correctly.
Not sure if this will fix the flip/rotation errors but I'm sure it is a
step in the right direction.

Sean

Bob Powell

1/12/2005 7:22:00 AM

0

You can accurately obtain the bounding rectangle of a curve by drawing the
curve to a GraphicsPath and then flattening it. The result is a set of
discrete points rather than a bezier so you can get the bounding rectangle
from the points contained using a simple x,y minimum and maximum routine.

HTH

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tips...

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/f...

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.





<sean.slavin@wonderware.com> wrote in message
news:1105482731.458016.202620@c13g2000cwb.googlegroups.com...
>I think I figured this out. For those that are curious, it looks as
> though the curve tension affects the bounding rectangle. Higher
> tensions result in rectangles being drawn(calculated) beyond the curve.
> Tensions close to 0.0 are the opposite. The default tension of .5
> appears to calculate the bounding rectangles for the curves correctly.
> Not sure if this will fix the flip/rotation errors but I'm sure it is a
> step in the right direction.
>
> Sean
>


(sean)

1/12/2005 5:32:00 PM

0

Bob,

Thank you for Flatten(). That appears to help. Are you saying that I
shouldn't just call GetBounds() on the path?

Sean

Bob Powell

1/12/2005 6:46:00 PM

0

The bounds are taken from the points in the path, not neccesarily where the
curve extends past the points due to bezier line tension. This is deceptive.

You can flatten the path to get the bounds but use the original shape with
the beziers in for better scalability and flexibility.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tips...

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/f...

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.





<sean.slavin@wonderware.com> wrote in message
news:1105551094.567450.187190@f14g2000cwb.googlegroups.com...
> Bob,
>
> Thank you for Flatten(). That appears to help. Are you saying that I
> shouldn't just call GetBounds() on the path?
>
> Sean
>