using graphic to draw pentagon - c#

Currently i'm trying to draw a pentagon with graphic but to no avil as i'm unable to get the points. So far I had tried it to draw out a triangle which I had succeeded , code is as below;
SolidBrush sb = new SolidBrush(painting);
PointF point1 = new PointF(25, 350);
PointF point2 = new PointF(450, 350);
PointF point3 = new PointF(225, 50);
PointF[] curvePoints = { point1, point2, point3 };
g.FillPolygon(sb, curvePoints);
paintstart = false;
(using triangle value as base)i thought of adding two more points (since pentagon have 5 sides) and my code for pentagon goes like this
SolidBrush sb = new SolidBrush(painting);
PointF point1 = new PointF(25, 350);
PointF point2 = new PointF(450, 350);
PointF point3 = new PointF(225, 50);
PointF point4 = new PointF(10, 150);
PointF point5 = new Point(475, 150);
PointF[] curvePoints = { point1, point2, point3, point4,point5};
g.FillPolygon(sb, curvePoints);
paintstart = false;
after added the 5th point, the whole shape figure goes distorted while 4th point remain alright.
Any idea why after adding the 5th point, the whole shape goes distorted? I add + 15 value to the x value of the pentagon base(450)and y axis remained the same.
Any help is appreciated thanks !

The order of the points matters when you have more than three points:
PointF[] curvePoints = { point1, point2, point5, point3, point4 };

Related

How to get Bounds of New Region made with PointsF?

I am trying some simple collisions like this:
if (Object1.bounds.intersectWith(Object2.bounds){ }
Now I am defining a new region for each object using pointsf like this :
using (GraphicsPath gp = new GraphicsPath())
{
// Create points that define polygon.
PointF point1 = new PointF(this.Width/2, 0);
PointF point2 = new PointF(this.Width, this.Height/2);
PointF point3 = new PointF(this.Width / 2, this.Height);
PointF point4 = new PointF(0, this.Height/2);
PointF[] curvePoints =
{
point1,
point2,
point3,
point4,
};
gp.AddLines(curvePoints.ToArray());
this.Region = new Region(gp);
}
But, the intersectionWith statement is only detecting the rectangle bounds. I try a code similar to this but its not working, how can I get the new region boundaries (triangle) in order to make it work?
if (Object1.region.Bounds.IntersectsWith(Object2.region.Bounds))
(This doesn't work obviously);
Thanks in advance

How to convert an Array of Point to a Rectangle

I have an Array of Point and I want to make it a Rectangle.
There are 4 points. I draw these points as a Polygon and the output is a Rectangle.
How can I draw these four points as a Rectangle?
Meaning, I want to get the width and height of these points.
These are my four points:
p1 :48.5, 196.5
p2 :997.5, 196.5
p2 :997.5, 692.5
p2 :48.5, 692.5
So I want something like this:
RectangleF rec = new RectangleF(x, y, width, hight);
My x and y is p1.x , p1.y
How can I get the width and height from these points?
RectangleF rec = new RectangleF(p1.x, p1.y, width, hight);
Assuming that:
Point p1 = new Point(48.5, 196.5);
Point p2 = new Point(997.5, 196.5);
Point p3 = new Point(997.5, 692.5);
Point p4 = new Point(48.5, 692.5);
You can create Rectangle as follow:
RectangleF rec = new RectangleF(p1.X, p1.Y, p2.X - p1.X, p3.Y - p1.Y);
Alternative solution using GraphicsPath()
using (GraphicsPath path = new GraphicsPath())
{
PointF[] points = new PointF[] {
new PointF(48.5f, 196.5f),
new PointF(997.5f, 196.5f),
new PointF(997.5f, 692.5f),
new PointF(48.5f, 692.5f),
};
path.StartFigure();
path.AddPolygon(points);
path.CloseFigure();
e.Graphics.DrawPath(new Pen(Color.Black, 2), path);
};

Drawing Polygon C# OOP can't find Array

I am trying to draw an Polygon in my WFA but it can't find the "curvePoints"in my class which are definetly there
class Driehoek : Figuur
{
Pen blackPen = new Pen(Color.Black, 3);
public void driehoek(Point p)
{
//this.x = 120;
//this.y = 50;
//this.width = 100;
//this.height = 100;
Point point1 = new Point(100, 150);
Point point2 = new Point(150, 100);
Point point3 = new Point(200, 150);
Point[] curvePoints =
{
point1,
point2,
point3,
};
}
public override void Teken(Graphics g)
{
g.DrawPolygon(blackPen, curvePoints);
// Error here is: The name 'curvePoints' does not exist in the current context
}
}
Create a new Point[] in your class, right after you make the Pen:
class Driehoek
{
Pen blackPen = new Pen(Color.Black, 3);
Point[] curvePoints;
}
Then, modify your function slightly so that you assign an array to the existing array, instead of creating a new one:
public void driehoek(Point p)
{
//this.x = 120;
//this.y = 50;
//this.width = 100;
//this.height = 100;
Point point1 = new Point(100, 150);
Point point2 = new Point(150, 100);
Point point3 = new Point(200, 150);
//Changed Point[] curvePoints to just curvePoints
curvePoints =
{
point1,
point2,
point3,
};
}
which are definetly there
It's a lie! Your array is in another method (out of "Teken" scope), not in class.

c# can you get position from axis without having a form?

I have a WinForm application that I would like to convert to a service. It has two mscharts on it. I don't need to see those charts anymore because the application creates bitmaps out of them. I am drawing a line on one chart depending on data from another chart, like this:
// Get Graphics object from chart
Graphics graph = e.ChartGraphics.Graphics;
// Convert X and Y values to screen position
float pixelYMax = (float)e.ChartGraphics.GetPositionFromAxis("Default", AxisName.Y, CreatininePoint.YValues[0]);
float pixelXMax = (float)e.ChartGraphics.GetPositionFromAxis("Default", AxisName.X, CreatininePoint.XValue);
float pixelYMin = (float)e.ChartGraphics.GetPositionFromAxis("Default", AxisName.Y, CreatininePoint.YValues[0]-20);
float pixelXMin = (float)e.ChartGraphics.GetPositionFromAxis("Default", AxisName.X, CreatininePoint.XValue);
PointF point1 = PointF.Empty;
PointF point2 = PointF.Empty;
// Set Maximum and minimum points
point1.X = pixelXMax;
point1.Y = pixelYMax-10;
point2.X = pixelXMin;
point2.Y = 84;
// Convert relative coordinates to absolute coordinates.
point1 = e.ChartGraphics.GetAbsolutePoint(point1);
point2 = e.ChartGraphics.GetAbsolutePoint(point2);
float[] dashValues = { 4,2 };
Pen blackPen = new Pen(Color.Black, 1);
blackPen.DashPattern = dashValues;
//e.Graphics.DrawLine(blackPen, new Point(5, 5), new Point(405, 5));
//graph.DrawLine(blackPen, point1, point2);
// Draw connection line
graph.DrawLine(new Pen(Color.Black, 2), point2, point1);
// Create string to draw.
//String drawString = creatininept + " ng/mL";
String drawString = "67 ng/mL";
// Create font and brush.
Font drawFont = new Font("Arial", 7);
SolidBrush drawBrush = new SolidBrush(Color.Black);
// Create point for upper-left corner of drawing.
PointF drawPoint = new PointF(point1.X,point1.Y+5);
// Draw string to screen.
graph.DrawString(drawString, drawFont, drawBrush, drawPoint);
is it possible to do this without having a form so that we can have a service running?
Bitmap canvas = new Bitmap(600, 480);
Graphics graph = Graphics.FromImage(canvas);
.. then proceed with drawing on graph.

c# drawing text over an object

i am drawing a dashed line over an object using this method:
// Get Graphics object from chart
Graphics graph = e.ChartGraphics.Graphics;
PointF point1 = PointF.Empty;
PointF point2 = PointF.Empty;
// Set Maximum and minimum points
point1.X = -110;
point1.Y = -110;
point2.X = 122;
point2.Y = 122;
// Convert relative coordinates to absolute coordinates.
point1 = e.ChartGraphics.GetAbsolutePoint(point1);
point2 = e.ChartGraphics.GetAbsolutePoint(point2);
// Draw (dashed) connection line
float[] dashValues = { 4, 2 };
Pen dashPen= new Pen(Color.Yellow, 3);
dashPen.DashPattern = dashValues;
graph.DrawLine(dashPen, point1, point2);
and i would like to know whether it is possible to write text over an object the same way/.??
You should look at the DrawString method.
Yes you can accomplish this in Graphics.DrawString

Categories

Resources