I know that you can use the scale(Matrix) to rezise a graphics path but when I use it the X cooridinate changes and so does the Y cooridinate, How can I get my scaling so that if the X cooridinate stays at 10 and the Y cooridinate stays at 10 but the width and height change to what ever I need?
Thanks
Loftty
Last edited by loftty; Jul 28th, 2006 at 05:38 AM.
Basically, for each coordinate of each point in your graphics path, that coordinate is multiplied by the scale value you give it.
Sounds like you have designed a path that is not centred around 0,0.
This is tricky to explain so i'll draw it...
If you design a graphicspath with 0,0 completely outside your shape then when you scale it the whole shape will appear to move off to one side. However if you design it with 0,0 inside the shape then the shape will just get larger when you scale it and it won't move off to one side.
This is an over simple way to describe it but you get what I mean.
If you were to design a hand for a clockface for example, then you'd make sure that your design had 0,0, at the pivot-point of the design. then you can simply rotate (and/or scale) the GP and it will behave just like a clock hand and will not drift off centre.
Last edited by wossname; Jul 20th, 2006 at 06:53 AM.
Its not something tha I can give you a sample of really. All you need to do is alter the coordinates of your original shape. Can you post the portion of your code where you are adding shapes to your graphics path? Then Ii can give you a hint as to how to alter it.
I am not at my computer at the mo but I do have another question if that is ok, do you know some where I can get hold of lots of shapes? also is it possible to create a circle with a gradient backcolor and inside that circle have another circle that is white?
Thanks for you help so far, I will post an example of my code when I get home.
The thing is that you should not define your shape to be at any particular location when you are designing it. It should be at or around 0,0, because then you will be able to translate it and scale it without any special maths to rectify the problems you are having.
I would suggest you use new rectangle(-15, -15, 30, 30) Because this straddles the rectangle exactly over 0,0 and thus you can rotate and scale and translate easily before you draw it in its new location.
try this on an empty form with a button in the top right of the form....
Code:
GraphicsPath gp = new GraphicsPath();
gp.AddRectangle( new Rectangle( -15, -15, 30, 30 ) );
this.CreateGraphics().DrawPath( new Pen( Color.Blue, 3 ), gp );
Matrix m = new Matrix();
m.Translate( 100, 100 );
m.Scale( 2, 2 );
m.Rotate( 20 );
gp.Transform( m );
this.CreateGraphics().DrawPath( new Pen( Color.Red, 3 ), gp );
I can see what you mean, but what happens when I do move the position of the shape to a new location of 50,50, and how would I do it on a shape like the polygon image you attached?
Thanks
Loftty
Last edited by loftty; Jul 24th, 2006 at 11:53 AM.
Normally when you want to draw many copies of a shape you'd translate() it with a matrix object, then draw it, then reset it. Then repeat this for each location you want to draw the shape at.
I do really appriciate your help so far, but I am still not able to scale a graphics path could you give me a example of a polygon shape that is not 0,0 location and the scale changes to the value of a trackbars value?
Thanks for that, it was most helpful, I have have been having a good play with it and am getting some good results, Thank you.
In this part of the code when I rotate it rotates the path from the top left hand corner, how can I get it to rotate from the center of the path, maybe I'm just missing a small thing here.
Code:
PointF center = new PointF((int)temp.GetBounds().X + ((int)temp.GetBounds().Width / 2), (int)temp.GetBounds().Y + ((int)temp.GetBounds().Height / 2));
//we'll be drawing the shape in the middle of the picture box...
m.Translate((int)_myShape.GetBounds().X + ((int)_myShape.GetBounds().Width ) >> 1, (int)_myShape.GetBounds().Y + ((int)_myShape.GetBounds().Height ) >> 1);
m.RotateAt(rotation, center);
m.Scale(scaleWidth, scaleHeight);
temp.Transform(m);
drawPaths();
Look at the image I attached. Look at how I defined the shape... 0,0 lies in the centre (more or less) of the arrowhead. That means it looks like it is spinning around on the spot.
OR...
You can just apply a one-time translation to your path before you store it safely away, make that translation move your shape so that it lies all around 0,0 and not just to the south-east of it. (in other words translate(-halfwidth, -halfheight);)
I have managed to rotate the path from the center of the shape, I have one more question or problem, if you have a look at the project and hit drawshape then rotate it the shape rotates fine but when I change the width or height the shapes looks more like it gets skewed, This could be right but I dont know if it is or not.
And if I create an ellipse it does not seem to rotate.
You were making things too complicated due to you not defining your rectangle in the right place. This is what I mean about your coordinates in the AddRectangle() call.
I've deleted some of the unnecessary code and altered your rectangle so that 0,0 is in the centre of the shape.
If you have your shape eccentric with regard to 0,0 then any scaling will tend to look weird.