How can I draw a filleeliipse on pictureBox corners? - c#

I'm trying to draw one ellipse at 0,0 and it's working but on the other side at pictureBox1.Width it's not drawing anything :
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.FillEllipse(Brushes.Purple, 0, 0, 5, 5);
e.Graphics.FillEllipse(Brushes.Purple, pictureBox1.Width, 0, 5, 5);
}
The result is one ellipse filled on the left side at 0,0 but on the right side nothing.
There should be small purple point also near the right yellow one.
And then to draw filled ellipse on all the 4 corners on the pictureBox1.

The result is one ellipse filled on the left side at 0,0 but on the right side nothing. There should be small purple point also near the right yellow one.
This is because it's off the screen a bit, you'll need to adjust the X coordinate.
e.Graphics.FillEllipse(Brushes.Purple, pictureBox1.Width - 5, 0, 5, 5);
If you'd like all four corners:
int circleSize = 5;
e.Graphics.FillEllipse(Brushes.Purple, 0, 0, circleSize, circleSize); //top left
e.Graphics.FillEllipse(Brushes.Purple, pictureBox1.Width - circleSize, 0, circleSize, circleSize); //top right
e.Graphics.FillEllipse(Brushes.Purple, pictureBox1.Width - circleSize, pictureBox1.Height - circleSize, circleSize, circleSize); //bottom right
e.Graphics.FillEllipse(Brushes.Purple, 0, pictureBox1.Height - circleSize, circleSize, circleSize); //bottom left

Related

MonoGame VertexPositionColor draws in incorrect place

I'm trying to make a rendering library for monogame and I'm currently working on drawing 2D polygons. However, The positions don't make any sense. Somehow, drawing them at (0, 0, 0), (100. 0, 0), (0, 100, 0), and (100, 100, 0) doesn't reach the top-left coordinate (0, 0). How do I fix this?
My Code:
BasicEffect basicEffect = new BasicEffect(GraphicsDevice);
VertexPositionColor[] vert = new VertexPositionColor[4];
vert[0].Position = new Vector3(0, 0, 0);
vert[1].Position = new Vector3(100, 0, 0);
vert[2].Position = new Vector3(0, 100, 0);
vert[3].Position = new Vector3(100, 100, 0);
short[] ind = new short[6];
ind[0] = 0;
ind[1] = 2;
ind[2] = 1;
ind[3] = 1;
ind[4] = 2;
ind[5] = 3;
foreach (EffectPass effectPass in basicEffect.CurrentTechnique.Passes)
{
effectPass.Apply();
GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionColor>(
PrimitiveType.TriangleList, vert, 0, vert.Length, ind, 0, ind.Length / 3);
}
RESULT: https://imgur.com/GkyqmlY
MonoGame uses a different origin for 2D and 3D coordinate systems. In 2D, (0, 0) is top-left corner, and Y increases toward the bottom of the screen. In 3D, (0,0,0) is the center of the screen, and the coordinate grid works very much like it does in mathematics - think 4 quadrants in math, if you "flatten" the z-axis.
You're drawing in Quadrant I. If you want the drawing to be based on top-left corner, you need to translate your vertices by -1/2 your viewport width and +1/2 your viewport height.

How to draw a rounded rectangle with width less than height? C#

I have been looking for quite a while for an already pre-made function for creating a rounded rectangle that doesn't cause tearing/glitching like the one below. Credits to #György Kőszeg. This function works fine if the rectangle is big enough. When you start making the rectangle smaller you run into issues like the below image. I am looking for an easy fix for this.
If this issue is on this website and I have missed it, I do apologize for re-asking. (I remember asking this a while back either on here or on another website and receiving an answer that worked amazingly) This issue has been paining me for quite awhile (again).
public static GraphicsPath RoundedRect(Rectangle bounds, int radius)
{
int diameter = radius * 2;
Size size = new Size(diameter, diameter);
Rectangle arc = new Rectangle(bounds.Location, size);
GraphicsPath path = new GraphicsPath();
if (radius == 0)
{
path.AddRectangle(bounds);
return path;
}
// top left arc
path.AddArc(arc, 180, 90);
// top right arc
arc.X = bounds.Right - diameter;
path.AddArc(arc, 270, 90);
// bottom right arc
arc.Y = bounds.Bottom - diameter;
path.AddArc(arc, 0, 90);
// bottom left arc
arc.X = bounds.Left;
path.AddArc(arc, 90, 90);
path.CloseFigure();
return path;
}
UPDATE:
In the meantime I have started to use the code below which just overrides the rectangle if the width is less than the height. This creates a perfect circle (the smallest the rounded rectangle can be without the glitching/tearing). A comment was placed on this stating I should not use "tearing" because its simply caused by the math which I understand but I really have no idea what else to call the glitchy rectangle in the image.
Basically I want a oval instead of a circle to correctly reflex the "Exp" value.
public static GraphicsPath RoundedRect(Rectangle bounds, int radius)
{
int diameter = radius * 2;
Size size = new Size(diameter, diameter);
Rectangle arc = new Rectangle(bounds.Location, size);
GraphicsPath path = new GraphicsPath();
//new code here//
if(bounds.Height >= bounds.Width)
{
bounds.Width = bounds.Height;
}
if (radius >= diameter) {
path.AddRectangle(bounds);
return path;
}
// top left arc
path.AddArc(arc, 180, 90);
// top right arc
arc.X = bounds.Right - diameter;
path.AddArc(arc, 270, 90);
// bottom right arc
arc.Y = bounds.Bottom - diameter;
path.AddArc(arc, 0, 90);
// bottom left arc
arc.X = bounds.Left;
path.AddArc(arc, 90, 90);
path.CloseFigure();
return path;
}
Something like this may work better. Try it with various values for curveSize. Note that curveSize must be between 1 and the minimum of rect.Width/4 and rect.Height/4:
public static GraphicsPath RoundedRect(Rectangle rc, float curveSize)
{
if (curveSize < 0 || curveSize > rc.Height / 4.0f || curveSize > rc.Width / 4.0f)
curveSize = 0;
var result = new GraphicsPath();
if (curveSize > 0)
{
float size4 = curveSize * 4;
result.AddArc(rc.Right - size4, rc.Top, size4, size4, 270, 90);
result.AddArc(rc.Right - size4, rc.Bottom - size4, size4, size4, 0, 90);
result.AddArc(rc.Left, rc.Bottom - size4, size4, size4, 90, 90);
result.AddArc(rc.Left, rc.Top, size4, size4, 180, 90);
result.CloseFigure();
}
else if (rc.Width > 0 && rc.Height > 0)
{
result.AddRectangle(rc);
}
return result;
}

C# Rotate Transform

I can rotate the Panel and Text 90º and it works for me. But rotating 180º doesn't work, I can't see the text. What can I do to fix it?
else if (m_orientation == AfyLabelOrientation.TurnedLeft90)
{
e.Graphics.TranslateTransform(0, this.Height - 5);
e.Graphics.RotateTransform(270);
if (!TextShadow_)
{
e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), new RectangleF(Padding.Left, Padding.Top, this.Height, this.Width));
}
else if (TextShadow_)
{
//Drawing text shadow
e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(Color.Gray), new RectangleF(Padding.Left + 1, Padding.Top - 1, this.Height, this.Width));
//Drawing text
e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), new RectangleF(Padding.Left, Padding.Top, this.Height, this.Width));
}
}
else if(m_orientation == AfyLabelOrientation.Overturned)//This don't work
{
e.Graphics.TranslateTransform(this.Width, 0);
e.Graphics.RotateTransform(180);
if (!TextShadow_)
{
e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), new RectangleF(Padding.Left, Padding.Top, this.Height, this.Width));
}
else if (TextShadow_)
{
//text shadow
e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(Color.Gray), new RectangleF(Padding.Left + 1, Padding.Top - 1, this.Height, this.Width));
//text
e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), new RectangleF(Padding.Left, Padding.Top, this.Height, this.Width));
}
}
If I got it, you need to translate to the object to maintain its center.
RotateTransform always rotates about the origin. So you need to first translate your centre of rotation to the origin, then rotate, then translate it back.
//move rotation point to center of image
g.TranslateTransform((float)this.Width/2, (float)this.Height / 2);
//rotate
g.RotateTransform(angle);
//move image back
g.TranslateTransform(-(float)this.Width/2,-(float)this.Height / 2);
It may be that what you are trying to rotate is in the left top corner of container. Then, rotation rotates around left top corner of your object, so 180 degrees rotation moves your object outside of view window.
________
|text |
_________
is rotated into something like:
_______
text| |
________
of course I'm not painting text rotated, but just trying to idicate its position. Move rotation point to the middle of text or move text by its width to the right after rotation to end with the text being put in correct place.

drawing lines bottom of form

How can I draw a horizontal box at the bottom of my viewport in OpenGL? This is drawing it at the top. What's wrong?
var H = window height;
var len = 20;
gl.Vertex3d(0, H - len, 0); //top left
gl.Vertex3d(Width, H - len, 0); //top right
gl.Vertex3d(Width, H + len, 0); //bottom left
gl.Vertex3d(0, H + len, 0); //bottom right
OpenGL's default coordinate space has Y pointing upward. You can change that, or you can change your vertices, depending on what you're going to do and what you're used to. In the case you've posted, where it's just a single quad, you can probably just change the vertices and do something like this:
gl.Vertex3d(0, len, 0); //top left
gl.Vertex3d(Width, len, 0); //top right
gl.Vertex3d(Width, -len, 0); //bottom left
gl.Vertex3d(0, -len, 0); //bottom right
If you're drawing a lot of other stuff, you can set the current transform matrix as you need to before drawing (and possibly restore it afterwards), by doing something along the lines of:
gl.matrixMode(GL_PROJECTION);
gl.ortho(left, right, top, bottom, near, far); // Note reversal of top and bottom

Half Rounded rectangle in C# Path

I've got question,
I have rounded rectangle. It's only rounded on right side, and straight on left. I want to make that in other way - left rounded, right straight.
What should I change?
int ArcWidth = 10 * 2;
int ArcHeight = 10 * 2;
int ArcX1 = Rect.Left;
int ArcX2 = Rect.Right -(ArcWidth + 1);
int ArcY1 = Rect.Top;
int ArcY2 = Rect.Bottom -(ArcHeight + 1);
path.AddArc(ArcX1, ArcY1, 1, 1, 180, 90); // Top Left
path.AddArc(ArcX2, ArcY1, ArcWidth, ArcHeight, 270, 90); //Top Right
path.AddArc(ArcX2, ArcY2, ArcWidth, ArcHeight, 360, 90); //Bottom Right
path.AddArc(ArcX1, ArcY2, 1, ArcHeight, 90, 90); //Bottom Left
Switch the sides that are rounded. Looks like you have all of your sides laid out. Switch all your numbers.
I found solution here:
http://tech.pro/tutorial/656/csharp-creating-rounded-rectangles-using-a-graphics-path
The way is to draw a Line, not Arc... (yes I know that was obvious)

Categories