.NET Graphics.FillEllipse() - c#

So the .NET documentation says that this creates a filled ellipse with the UPPER LEFT CORNER at the X,Y coordinate specified.
But I need the ellipse to be CENTERED on the X,Y coordinate I supplied.
How do I do this?
Thanks!

From the desired center point, decrease X by half of the width and decrease Y by half of the height.

Related

Drawing an arc with negative rectangle c#

I'm trying to draw an arc of a circle in C#. The general code to do this is:
e.DrawArc(pen, x, y, d, d, startAngle, endAngle - startAngle);
Where x and y indicate the upper left corner of the Rectangle and d the width of the Rectangle (also the diameter of the arc).
The problem I'm facing is that sometimes I need to draw an arc whoose rectangle x and y values lie outside the bitmap I'm drawing onto (they may even be negative), and thus the arc is not being drawn at all.
Any ideas?
Thanks!
Okay, so I solved it using the Graphics.DrawCurve(pen, points[]) method. I calculated multiple points of the circle doing some math and plotted them as a curve. The result is pretty neat (around 20 points I can't tell that it's not an actual circle), and it works no matter where I want to draw the arc.

Calculate rectangle's points from diagonal in 3D

I know 3 points in a 3D plane. Two points are the ends of a diagonal and an other one which is random point on the plane. How can I calculate the two other points of a rectangle from the known diagonal line? (Later I will use the points to calculate the perimeter of the rectangle in C#.)
There's no single right answer. All you can calculate using a diagonal and a random point on the plane is a whole sets of possible answers.
Imagine rotating the diagonal to create a circle - now every second line inscribed in that circle and going through the center can be the second diagonal. The only limit is your third point.
Since you know the end points of the diagonal, you can calculate the length of the diagonal; from there you can determine the rectangle side length; having diagonal coordinates and the side length, you can determine the other two points of the rectangle using add/subtraction.

User-Resizable and User-Rotatable shapes on Canvas with WPF

I'm currently creating a drawing software using WPF Shapes on canvas.
I've created a system allowing the user to move and rotate shapes on a Canvas using a transparent canvas upon the shape (which rotate with the shape) :
The green point is used to rotate the shape, the blue zone upon the rectangle is used to move the shape. I'd like to use my 4 red points to re-size the shape.
But the shape is rotatable, so corners coordinates aren't completely relevant to resize the shape. It seems, in my opinion, to be relevant only if the rotation is equals to 0, because the Left-Top Corner can be the Bottom-Right one after a 180 degree rotation.
Right now I'm using a RotateTransform to achieve the rotation with a 0.5, 0.5 RenderTransformOrigin. I'd like to avoid the use of a ScaleTransform because I want to keep the StrokeThickness at the size it is.
All red dots are pseudo-draggable (using MouseDown, MouseMove, MouseUp events). I use a buffer point which gives me the delta in X and Y between two mouse events.
How can use the deltas to resize the shape, even if it is rotated or moved ?
You can use the deltas to resize the shape if it is rotated. The only thing you have to do is rotating the mousemovement either. As you can see:
The movement of the mouse from origin to location describes a 2-D-vector. You can rotate this vector mathematically by using this formula:
x' = cos(theta) * x - sin(theta) * y
y' = sin(theta) * x + cos(theta) * y
where x/y is the current location of the mouse relative to the origin of the resize and theta the angle of rotation which can be found in the RotateTransform-object (Angle-Property) of the shape. At this point I don't know exactly if you have to use -theta, because the vector has to rotate in the opposite direction.
You can pick x'/y' for calculating the deltas and resize the shape like if it wasn't rotated.
I did not implement this myself. This is just a general idea. Maybe I can serve with a little code if you try this and give feedback or specify the problem more deeply or update your question with some code.
Appendix:
Resizing the shape using the deltas should be easy if you can access the width- and height-property of the shape. You simply add/subtract the x-delta to/from width and/or add/subtract the y-delta to/from height, depending on the grabbed point. This isn't affected by the location of the shape within the canvas.
Maybe you have to adjust the Canvas.Left/Canvas.Top-Property of the shape. I.e. if the user grabs the left upper point and resizes it to left/up, you should subtract the deltas from left and top porperty as well. Otherwise it will expand to right/down.

Silverlight Transforms Images C#

I have an image on which I may use a ScaleTransform to increase the image size by 25%. I also have a rectangle which sits on top of the image highlighting a particular area. When I scale the image I want the rectangle to scale as well and highlight the same area as before. Scaling the rectangle itself isn't the issue, it's getting the rectangle into the correct position so that it highlights the same area. How do I do this? Is there a mathematical formula of some description that can be used to calculate its correct position?
Apply the same scaling factor to the X and Y offsets of the rectangle.

Draw Points onto canvas using an offset?

I have an array of Point variables. When drawn using Graphics.DrawLine, they create the expected image. My problem is that 0,0 is actually the center of the image (not the top left of my canvas as expected. My X and Y coordinates in the Points can contain negative numbers.
When I try to draw this to my Image, of course I get 1/4 of the total image as the remainder is drawn outside the bounds of my canvas. How do I center this drawing correctly onto my canvas?
I know the dimensions of the image I want to draw. I know where 0,0 is (width / 2, height / 2).
I suppose I can translate each and every single Point, but that seems like the hard way to do this.
TranslateTransform() can map coordinates for you if you setup a transformation during your drawing handlers.
Graphics.TranslateTransform # MSDN
Or, map your coordinates by adding half the width and half the height of the desired viewing area to each coordinate.
Also, you may need to scale your coordinates. You may use Graphics.ScaleTransform to do this.
Graphics.ScaleTransform # MSDN
If you don't wish to use this, then you should divide X coordinates by the percent amount you wish to stretch the width, and divide Y coordinates by the percent amount you wish to stretch the height. This gives us 1 for 100%, 1.2 for 120%, 0.8 for 80%, etc.
Welcome to the Windows' version of the Cartessian Plane. Your last statement is correct. You do have to offset each and every point. The only real help you can give yourself is to make the offset logic a separate method to clean up your main drawing code.
When creating the array, add an offset to each x value equal to half of the width and an offset to y equal to half of the height. That way when the points are drawn, they're in the expected position.

Categories