I am very new to c# form and i would like to write some letters in a specific location. As you can see in the picture i have attached I've drawn a curved Line with a X and Y axis to scale it with. I would like to write the letter X on the edge of the horizontal line and Y on the top Edge of the vertical line. Also is there any possible ways to assign values on the line as well?
protected override void OnPaint(PaintEventArgs e)
{
float a = 1, b = 5, c = -4;
double x1, x2, x3, x4, x5, x6, y1, y2, y3, y4, y5, y6, x7, y7, delta;
delta = (b * b) - (4 * a * c);
x1 = ((b * (-1)) + Math.Sqrt(delta)) / (2 * a);
x6 = ((b * (-1)) - Math.Sqrt(delta)) / (2 * a);
y6 = a * (x6 * x6) + (b * (x6)) + c;
y1 = a * (x1 * x1) + (b * (x1)) + c;
x2 = 3;
y2 = a * (x2 * x2) + (b * (x2)) + c;
x3 = -3;
y3 = a * (x3 * x3) + (b * (x3)) + c;
x4 = 5;
y4 = a * (x4 * x4) + (b * (x4)) + c;
x5 = -10;
y5 = a * (x5 * x5) + (b * (x5)) + c;
x7 = 0;
y7 = a * (x7 * x7) + (b * (x7)) + c;
int cx1 = Convert.ToInt32(x1);
int cx2 = Convert.ToInt32(x2);
int cx3 = Convert.ToInt32(x3);
int cy1 = Convert.ToInt32(y1);
int cy2 = Convert.ToInt32(y2);
int cy3 = Convert.ToInt32(y3);
int cx4 = Convert.ToInt32(x4);
int cy4 = Convert.ToInt32(y4);
int cx5 = Convert.ToInt32(x5);
int cy5 = Convert.ToInt32(y5);
int cx6 = Convert.ToInt32(x6);
int cy6 = Convert.ToInt32(y6);
int cx7 = Convert.ToInt32(x7);
int cy7 = Convert.ToInt32(x7);
Graphics g = e.Graphics;
int deltaX = 300;
int deltaY = 300;
g.TranslateTransform(deltaX, deltaY);
float factor = 2.5f;
Matrix m = new Matrix();
m.Scale(factor, factor);
g.MultiplyTransform(m);
Pen aPen = new Pen(Color.Blue, 1);
aPen.DashStyle = DashStyle.DashDot;
Pen bPen = new Pen(Color.Green, 1);
bPen.EndCap = LineCap.ArrowAnchor;
Pen cPen = new Pen(Color.Green, 1);
cPen.StartCap = LineCap.DiamondAnchor;
Point point1 = new Point(cx1, -cy1);
Point point2 = new Point(cx2, -cy2);
Point point3 = new Point(cx3, -cy3);
Point point4 = new Point(cx4, -cy4);
Point point5 = new Point(cx5, -cy5);
Point point6 = new Point(cx6, -cy6);
Point pointa = new Point(20, -50);
Point pointb = new Point(40, -30);
Point pointc = new Point(60, -70);
Point[] Points = { point5, point3, point1, point2, point4 };
Point[] Pointss = { pointa, pointb,pointc };
g.DrawCurve(new Pen(Color.Red, 1), Pointss);
g.DrawCurve(aPen, Points);
g.DrawLine((cPen), new Point(cx7, -100), new Point(cx7, 100));
g.DrawLine((bPen), -100, 0, 100, 0);
check my sample. I hope it helps.
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace GraphicsForm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
Invalidate();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
var g = e.Graphics;
var w = ClientRectangle.Width;
var h = ClientRectangle.Height;
var midY = h/2;
var midX = w/2;
var linePen = new Pen(Brushes.Red, 1)
{
StartCap = LineCap.DiamondAnchor,
EndCap = LineCap.DiamondAnchor
};
//horizontal line
g.DrawLine(linePen, 0, midY, w, midY);
var font = Font;
var measureStringX = g.MeasureString("x", font);
g.DrawString("x", font, Brushes.Black, w - measureStringX.Width - 2, midY + 2);
//vertical line
g.DrawLine(linePen, midX, 0, midX, h);
g.DrawString("y", font, Brushes.Black, midX + 2, 2);
//horizontals&vertical marks
const float marksCount = 12f;
var wx = w / marksCount;
var hx = h / marksCount;
var markPen = new Pen(Brushes.Red, 1);
for (int i = 1; i < marksCount; i++)
{
g.DrawLine(markPen, i * wx, midY, i * wx, midY + 5);
g.DrawLine(markPen, midX, hx * i, midX + 5, hx * i);
}
}
}
}
"Check out DrawString on the graphics object. https://msdn.microsoft.com/en-us/library/system.drawing.graphics.drawstring.aspx – dbugger"
Answered.
Related
I am trying to spread panels (9 in my example) on a circle that I have drawn.
I am using c# winforms.
I have tried many variations of my code but I'm not getting what I want and started to get confused.
Eventually I want something like that:
I am not really sure how to put the center of my panels on the corresponding points on the circle using the angles.
Here's my code:
public partial class Form1 : Form
{
List<Panel> plist = new List<Panel>();
Rectangle circ_rect = new Rectangle();
const int Num_Screens = 9;
const int margin = 15;
public Form1()
{
InitializeComponent();
WindowState = FormWindowState.Maximized;
}
private void Generate_Panels()
{
for (int i = 0; i < 9; i++)
{
Panel p = new Panel();
p.BackColor = Color.LightSkyBlue;
p.Size = new Size(250, 150);
p.BorderStyle = BorderStyle.FixedSingle;
p.Name = "panel_" + ((i + 1).ToString());
plist.Add(p);
}
}
private void Generate_Circle()
{
//Create panels
Generate_Panels();
//Set circle coord
Point circ_center = new Point(Width / 2, Height / 2);
Size circ_Size = new Size(Height - margin, Height - margin);
circ_center = new Point((circ_center.X - (circ_Size.Width / 2)),
(circ_center.Y - (circ_Size.Height / 2)));
circ_rect = new Rectangle(circ_center, circ_Size);
float radius = circ_Size.Width / 2;
float angle = 0.0f;
Point loc = Point.Empty;
Point rect_center = Point.Empty;
for (int i = 0; i < plist.Count; i++)
{
rect_center = new Point((plist[i].Width / 2), (plist[i].Height / 2));
angle = 360 * ((i + 1f) / 9);
loc.X = (int)(radius * Math.Cos(angle * Math.PI / 180)) + circ_center.X;
loc.Y = (int)(radius * Math.Sin(angle * Math.PI / 180)) + circ_center.Y;
plist[i].Location = new Point(loc.X - (plist[i].Width / 2) + circ_rect.X,
loc.Y - (plist[i].Height / 2) + circ_rect.Y);
this.Controls.Add(plist[i]);
}
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
e.Graphics.DrawEllipse(Pens.Red, circ_rect);
}
private void Form1_Load(object sender, EventArgs e)
{
Generate_Circle();
}
}
Having r as radius of a circle with center of (0,0) in a Cartesian coordinate system, we can calculate coordinate of a on the circle based on the angle:
x = r * cos(degree) and y = r * sin(degree)
In C# Sin and Cos methods, accept radians, so we should convert degree to radians using the following formula.
radians = Math.PI * degree / 180.0
The next step is converting the Cartesian coordinate system values to the form coordinate values:
panel.X = x + center.X - panel.Width/2
panel.Y = center.Y - y - panel.Height/2
The next step is calculating the angles. You can set angles manually or you can calculate them by setting an angle as start angle (like 90) and adding a value (like 40, 360/count) as step to the angles.
Example
public partial class Form1 : Form {
Rectangle circle;
List<Panel> panels;
List<int> angles;
public Form1() {
InitializeComponent();
ResizeRedraw = true;
angles = Enumerable.Range(0, 9).Select(x => 90 + x * 40).ToList();
panels = Enumerable.Range(0, 9).Select(x => new Panel() {
Size = new Size(100, 40),
BackColor = Color.LightSkyBlue
}).ToList();
this.Controls.AddRange(panels.ToArray());
}
protected override void OnLayout(LayoutEventArgs levent) {
base.OnLayout(levent);
int padding = 50;
int radius = Math.Min(ClientSize.Width, ClientSize.Height) / 2 - padding;
Point center = new Point(ClientSize.Width / 2, ClientSize.Height / 2);
circle = new Rectangle(center.X - radius, center.Y - radius,
2 * radius, 2 * radius);
for (int i = 0; i < 9; i++) {
var x = (int)(radius * Math.Cos(Math.PI * angles[i] / 180.0)) + center.X;
var y = center.Y - (int)(radius * Math.Sin(Math.PI * angles[i] / 180.0));
panels[i].Left = x - (panels[i].Width / 2);
panels[i].Top = y - (panels[i].Height / 2);
}
}
protected override void OnPaint(PaintEventArgs e) {
base.OnPaint(e);
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.DrawEllipse(Pens.Red, circle);
}
}
I want to draw sin(θ)*cos(θ), but it doesn't work.
I can draw sin or cos,
but I want to draw sin(θ)*cos(θ) together.
Here is my code
private void button1_Click(object sender, EventArgs e)
{
Graphics drw = this.CreateGraphics();
Pen pen = new Pen(Brushes.Black, 7.0f);
float x1 = 0;
float y1 = 0;
float xoy = 200;
float ef = 20;
for (double i=0;i<40;i+=1)
{
double radi = (float)(i * 180 / Math.PI);
float temp = (float)Math.Cos(radi)*(float)Math.Sin(radi);
drw.DrawLine(pen, x1 * ef, y1 * ef + xoy, ef * (float)i, temp * ef + xoy);
x1 = (float)i;
y1 = temp;
}
}
And I want this result:
You may find it easier to look at the corresponding Parametric Equations.
private void Form1_Paint(object sender, PaintEventArgs e)
{
var g = e.Graphics;
double pi = Math.PI;
int n = 100;
var t = Enumerable.Range(0, n).Select(p => p * 2 * pi / n).ToArray();
var x = t.Select(p => Math.Sin(2 * p) * Math.Cos(p)).ToArray();
var y = t.Select(p => Math.Sin(2 * p) * Math.Sin(p)).ToArray();
Pen pen = new Pen(Brushes.Black, 3);
int scale = 100;
int shift = 100;
for (int i = 0; i < n - 1; i++)
{
g.DrawLine(pen, scale*(float)x[i] + shift,
scale*(float)y[i] + shift,
scale*(float)x[i + 1] + shift,
scale*(float)y[i + 1] + shift);
}
}
Actually, the real function you are looking for is a little bit different... see an example here. Looking at this article about polar flowers, I'm sure it will get pointed to the right direction, and it also contains a full working source code.
Just an example, supposing you use a panel in your form on which to draw the polar flower:
panel.OnPaint += Panel_Paint;
private void Panel_Paint(Object sender, PaintEventArgs e)
{
Double scale = ((Panel)sender).Width / 2.0d;
Double repetitions = Math.Round(scale, 0);
Double basis = (2.0d * Math.PI) / scale;
Double petals = 2.0d;
using (Graphics g = e.Graphics)
{
using (Pen pen = new Pen(Brushes.Red, 2.0f))
{
for (Double i = 0.0f; i < (repetitions - 1); ++i)
{
Double t0 = i*basis;
Double t1 = (i + 1)*basis;
Double x0 = Math.Sin(petals * t0) * Math.Cos(t0);
Double x1 = Math.Sin(petals * t1) * Math.Cos(t1);
Double y0 = Math.Sin(petals * t0) * Math.Sin(t0);
Double y1 = Math.Sin(petals * t1) * Math.Sin(t1);
g.DrawLine
(
pen,
(Single) ((scale*x0) + scale),
(Single) ((scale*y0) + scale),
(Single) ((scale*x1) + scale),
(Single) ((scale*y1) + scale)
);
}
}
}
}
The basic formulation states that if the petals variable value is:
even, then it represents half the amount of petals of the polar flower
odd, then it represents the amount of petals of the polar flower
so if you define Double petals = 2.0d;, you will obtain 4 petals... and if you define Double petals = 5.0d;, you will obtain 5 petals.
My program just prints a graph.
if I move my form outside the screen, my graph isn't printed. How can I fix it? I am a begginer in it.
Can you help with it?
Thank you.
private void button1_Click(object sender, EventArgs e)
{
Graphics gr ;
int m11, m12, m21, m22, dx, dy, ax, ay;
gr = pictureBox1.CreateGraphics();
int xmin = -5;
int xmax = 10;
int ymin = 0;
int ymax = xmax * xmax;
ax = pictureBox1.Size.Width / (xmax - xmin);
ay = pictureBox1.Size.Height / (ymax - ymin);
m11 = ax;
m12 = 0;
m21 = 0;
m22 = -ay;
dx = -xmin * ax;
dy = pictureBox1.Size.Height - ay * (-ymin);
System.Drawing.Drawing2D.Matrix M = new
System.Drawing.Drawing2D.Matrix(m11, m12, m21, m22, dx, dy);
gr.Transform = M;
int x = 0;
for (x = xmin; x < xmax; x++)
{
System.Drawing.Point p1 = new System.Drawing.Point(x, x * x);
System.Drawing.Point p2 = new System.Drawing.Point(x + 1, (x + 1) * (x + 1));
float brushSize = 0.2F;
System.Drawing.Pen pen = new
System.Drawing.Pen(System.Drawing.Brushes.Black, brushSize);
gr.DrawLine(pen, p1, p2);
}
I am making an automated floor plan generation desktop app. In this, at first I draw the polygon on points, using this method
public void DrawPolygonPointF(PaintEventArgs e) {
// Create pen.
Pen blackPen = new Pen(Color.Black, 3);
// Create points that define polygon.
PointF point1 = new PointF(50.0F, 50.0F);
PointF point2 = new PointF(100.0F, 25.0F);
PointF point3 = new PointF(200.0F, 5.0F);
PointF point4 = new PointF(250.0F, 50.0F);
PointF point5 = new PointF(300.0F, 100.0F);
PointF point6 = new PointF(350.0F, 200.0F);
PointF point7 = new PointF(250.0F, 250.0F);
PointF[] curvePoints =
{
point1,
point2,
point3,
point4,
point5,
point6,
point7
};
// Draw polygon curve to screen.
e.Graphics.DrawPolygon(blackPen, curvePoints);
}
NOTE: These points are not actual points, they are for only demo purpose. I am reading the points from a text file.
Now I need to generate a special type of grid.
In generating the grid the first step is detect corners and extend the corner lines.
How do I detect the corners of a polygon so can i move to next step of generating grid?
Corners are marked. I need to extend corner marked with black on horizontally left side and other one is to extend on right side till its touch the line.
A screenshot is attached.
Thanks in advance
In my understanding you are trying to extend edges, not corners.
the procedure could be:
Enumerate edges (each 2 adjacent points define an edge)
For each edge find if it vertical or horizontal (abs(x1-x2) > abs(y1-y2))
Find if edge can be extended, the horizontal (left/right) and the vertical (up/bottom)
bool CheckHorizontalExtensibilityToRight(Point[] curvePoints, Point corner)
{
return curvePoints.Any(cp=>cp.Y < corner.Y && cp.X < corner.X);
}
Try this example and see if you can adapt it to solve your problem...
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication
{
public partial class Form1 : Form
{
private struct LineSegment
{
private PointF _a, _b;
public PointF A { get { return _a; } }
public PointF B { get { return _b; } }
public LineSegment(PointF a, PointF b)
{
_a = a; _b = b;
}
public float GetLengthSquared()
{
var dx = _a.X - _b.X;
var dy = _a.Y - _b.Y;
return dx * dx + dy * dy;
}
public bool RectContains(PointF a)
{
var x = a.X;
var y = a.Y;
var x1 = _a.X;
var y1 = _a.Y;
var x2 = _b.X;
var y2 = _b.Y;
return (x1 < x2 ? x1 <= x && x2 >= x : x2 <= x && x1 >= x) && (y1 < y2 ? y1 <= y && y2 >= y : y2 <= y && y1 >= y);
}
public bool ExtendToIntersectWith(LineSegment b)
{
var x1 = _a.X;
var y1 = _a.Y;
var x2 = _b.X;
var y2 = _b.Y;
var x3 = b._a.X;
var y3 = b._a.Y;
var x4 = b._b.X;
var y4 = b._b.Y;
var a1 = y2 - y1;
var b1 = x1 - x2;
var c1 = x1 * y2 - x2 * y1;
var a2 = y4 - y3;
var b2 = x3 - x4;
var c2 = x3 * y4 - x4 * y3;
var d = a1 * b2 - b1 * a2;
if (d == 0)
return false;
var x = (c1 * b2 - b1 * c2) / d;
var y = (a1 * c2 - c1 * a2) / d;
var p = new PointF(x, y);
if (b.RectContains(p) && !RectContains(p))
{
if (new LineSegment(_a, p).GetLengthSquared() < new LineSegment(_b, p).GetLengthSquared())
_a = p;
else
_b = p;
return true;
}
return false;
}
}
public Form1()
{
InitializeComponent();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
PointF[] curvePoints =
{
/*
new PointF(50.0F, 50.0F),
new PointF(100.0F, 25.0F),
new PointF(200.0F, 5.0F),
new PointF(250.0F, 50.0F),
new PointF(300.0F, 100.0F),
new PointF(350.0F, 200.0F),
new PointF(250.0F, 250.0F)
*/
new PointF(30F, 10F),
new PointF(60F, 10F),
new PointF(60F, 20F),
new PointF(90F, 20F),
new PointF(90F, 60F),
new PointF(10F, 60F),
new PointF(10F, 40F),
new PointF(30F, 40F),
};
int n = curvePoints.Length;
LineSegment[] lineSegments = new LineSegment[n];
int i = 0;
for (; i < n - 1; ++i)
lineSegments[i] = new LineSegment(curvePoints[i], curvePoints[i + 1]);
lineSegments[i] = new LineSegment(curvePoints[i], curvePoints[0]);
for (i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
lineSegments[i].ExtendToIntersectWith(lineSegments[j]);
for (i = 0; i < n; ++i)
{
var lineSegment = lineSegments[i];
e.Graphics.DrawLine(Pens.Black, lineSegment.A, lineSegment.B);
}
//e.Graphics.DrawPolygon(Pens.Black, curvePoints);
}
}
}
I'm currently working on a 3d bin packing problem to which I want to represent my results as an image.
I have the results stored as a list of packing objects as follows
public class LoadedPackage
{
private PackingObject packingObject;
private int xloc, yloc, zloc;
private bool flipped = false;
}
public class PackingObject
{
private int ID, checkerMaster, height, width, depth, number;
}
I want to use the xloc,yloc,zloc and dimensions to draw the packages 1 at a time to build up an image. Is there some sort of image library way of doing this or am I going to be forced to use an openGL solution which seems a little overkill to me for just a simple image.
I was thinking of maybe using a isometric method using 2d gdi lib
In the end I opted for an isometric approach and added a method to each package to convert itself from a 3d coordinates over to isometric. I hope the following code helps someone else with a similar predicament!
An Isometric cube has 6 points so I return an array of 6 points. In reality i use 3 sub methods to return the 3 isometric polygons for the GDI lib to process but for the sake of this answer I'll just post the more general 6 point method.
public PointF[] convertCoords()
{
float x;
float y;
PointF p;
PointF[] pointFs = new PointF[7];
int x1 = 0;
int y1 = 0;
int z1 = 0;
double rads = Helpers.DegreeToRadian(30);
//point 0 in iso
x = ((float)Math.Cos(rads) * xloc) + ((float)Math.Cos(rads) * yloc);
y = ((float)Math.Sin(rads) * yloc) + zloc - ((float)Math.Sin(rads) * xloc);
y = (-y) + 250;
p = new PointF(x,y);
pointFs[0] = new PointF(p.X,p.Y);
//point 1 in iso
x1 = xloc + packingObject.Depth;
y1 = yloc;
z1 = zloc;
x = ((float)Math.Cos(rads) * x1) + ((float)Math.Cos(rads) * y1);
y = ((float)Math.Sin(rads) * y1) + z1 - ((float)Math.Sin(rads) * x1);
y = (-y) + 250;
p = new PointF(x, y);
pointFs[1] = new PointF(p.X, p.Y);
//point 2 in iso
x1 = xloc + packingObject.Depth;
y1 = yloc;
z1 = zloc + packingObject.Height;
x = ((float)Math.Cos(rads) * x1) + ((float)Math.Cos(rads) * y1);
y = (-((float)Math.Sin(rads) * x1)) + ((float)Math.Sin(rads) * y1) + z1;
y = (-y) + 250;
p = new PointF(x, y);
pointFs[2] = new PointF(p.X, p.Y);
//point 3 in iso
x1 = xloc;
y1 = yloc;
z1 = zloc + packingObject.Height;
x = ((float)Math.Cos(rads) * x1) + ((float)Math.Cos(rads) * y1);
y = (-((float)Math.Sin(rads) * x1)) + ((float)Math.Sin(rads) * y1) + z1;
y = (-y) + 250;
p = new PointF(x, y);
pointFs[3] = new PointF(p.X, p.Y);
//point 4 in iso
x1 = xloc;
y1 = yloc + packingObject.Width;
z1 = zloc + packingObject.Height;
x = ((float)Math.Cos(rads) * x1) + ((float)Math.Cos(rads) * y1);
y = (-((float)Math.Sin(rads) * x1)) + ((float)Math.Sin(rads) * y1) + z1;
y = (-y) + 250;
p = new PointF(x, y);
pointFs[4] = new PointF(p.X, p.Y);
//point 5 in iso
x1 = xloc + packingObject.Depth;
y1 = yloc + packingObject.Width;
z1 = zloc + packingObject.Height;
x = ((float)Math.Cos(rads) * x1) + ((float)Math.Cos(rads) * y1);
y = (-((float)Math.Sin(rads) * x1)) + ((float)Math.Sin(rads) * y1) + z1;
y = (-y) + 250;
p = new PointF(x, y);
pointFs[5] = new PointF(p.X, p.Y);
//point 6 in iso
x1 = xloc + packingObject.Depth;
y1 = yloc + packingObject.Width;
z1 = zloc;
x = ((float)Math.Cos(rads) * x1) + ((float)Math.Cos(rads) * y1);
y = (-((float)Math.Sin(rads) * x1)) + ((float)Math.Sin(rads) * y1) + z1;
y = (-y) + 250;
p = new PointF(x, y);
pointFs[6] = new PointF(p.X, p.Y);
return pointFs;
}