how to draw polygon including negative points ? in C# - c#

i am doing c# project. i have found a problem and unable to find answer of this problem that's why i am posting here/
i am implementing procedural floor plan generation in c#. at first i need to draw a polygon to on given points to generate grid. points can be positive or negative. points will be in floating.
From the Microsoft Documentation i am able to draw polygon for the floating positive points, but when i change points to negative it does not draw anything on form.
this method id drawing x,y coordinates 0,0 in the corner .
code example
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);
}
i have negative coordinates value for example pointF(300,-250) this method is not drawing nothing for negative coordinates value.
So please give me brief solution because i don't know much about drawing.
Thank you

In C# 0,0 is the top left corner of the screen. So negative values are off the left or top edge of your form. You need to transform your coordinates from your coordinate space to the form's. To place 0,0 dead center, you would do this:
int screenX = myX + windowWidth / 2;
int screenY = myY + windowHeight / 2;

Building off of the answer #pquest gave, and your newly added sample code, doing a "real" GDI transformation would look like this:
// 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);
<...snip...>
//Apply a translation Transformation to move 0,0 to the center of the
//window. You can now draw your points with negative values, without doing
//any addition to them.
int screenX = windowWidth / 2;
int screenY = windowHeight / 2;
e.Graphics.TranslateTransform((float) screenX, (float) screenY);
// Draw polygon curve to screen.
e.Graphics.DrawPolygon(blackPen, curvePoints);
GDI Transformations are very powerful. You can use them to move points around, flip points horizontally or vertically, scale things bigger or smaller, distort like a parallelogram, and even do rotations. All of these can be combined in to what is called a transformation matrix to get some very cool results.
More examples can be found on MSDN.

That's working for me to this problem.
you just need to add couple lines of code in your code.
int windowWidth = this.ClientSize.Width;
int windowHeight = this.ClientSize.Height;
int screenX = windowWidth / 2;
int screenY = windowHeight / 2;
e.Graphics.TranslateTransform((float)screenX, (float)screenY);

Related

how to find edge of an image with most distance from center?

i writing a sample program in c# who drawing some point in page
i set center point with calculate distance of point
but how can found most distance point from center point ?
sample code :
void draw(string label,float x,float y)
{
Graphics g = panel1.CreateGraphics();
Pen p = new Pen(Color.YellowGreen, 5);
Random randomGen = new Random(Convert.ToInt32(label));
KnownColor[] names = (KnownColor[])Enum.GetValues(typeof(KnownColor));
KnownColor randomColorName = names[randomGen.Next(names.Length)];
Color randomColor = Color.FromKnownColor(randomColorName);
SolidBrush s = new SolidBrush(randomColor);
g.FillEllipse(s, x * 1, y * 1, 10, 10);
}
The best and simplest approach for this problem what I can think of is:
1) scan the image/coordinate system horizontally and vertically
2) For each row/column store the lowest and the highest coordinates with non-zero intensity
that will be your boundary points
You can define the furthest point of an angle. This can solve the problem of convex hull, but this technique fits well only in a circle space, very like yours.
for each angle you have to find the furthest point and then assign it the red color.
You can Use as much angle as you want.
PSEUDO...
npoints = 10;
furthestPts = zeros(npoints );//Initialize vectors with 0
distances = zeros(npoints );
for each pt in points
angle = atan((pt.y - c.y)/pt.x - c.x) * 360 / ( 2 * pi); //degres
angle = (int) (angle/npoints); //we will have only 10 points separated by 36 degrees
d = distance(pt,center);
if(distances[angle] < d){
distances[angle] = d; //Updating furthest point
furthestPts.[angle] = (pt); //Updating furthest point
}
You will see that this algoritm has a few problems if the point are to far from center or if the points are to far from each other.

Matrix Transform of Rectangle after changing its bounds

I have a shape, for example a Rectangle which has the following bounds:
X = 100
Y = 100
Width = 100
Height = 100
I apply the following rotation to this rectangle using a new Matrix:
X = 100
Y = 100
Angle = 45
var transform = new Matrix();
transform.RotateAt(angle, point);
So the new Matrix has the following value:
0.7071068, 0.7071067, -0.7071067, 0.7071068, 150, -62.13202
I use this Matrix when I draw the Rectangle with Graphic:
protected override void OnPaint(PaintEventArgs e)
{
...
e.Graphic.Transform = transform;
g.DrawRectangle(Pen, bounds.X, bounds.Y, bounds.Width, bounds.Height);
}
The problem is the following: at a certain point I need to draw the same Rectangle but shifted by a certain offset, for example (50, 50). I have stored the Matrix transform and the Rectangle bounds. If I change only the bounds (adding the offset) the new Rectangle will be drawn in a wrong position, probably due to the previous rotation point.
How I have to change the Matrix in order to draw my Rectangle in the "right" position? that is, how can I retreive the right rotation point and the old rotation angle?
Try to add translation to the matrix using MatrixOrder.Prepend.
For offset (50, 50) it'll be:
transform.Translate(50, 50, MatrixOrder.Prepend);
Or create a separate matrix for this case:
var transformWithOffset = new Matrix();
transformWithOffset.Translate(50, 50);
transformWithOffset.RotateAt(angle, point);

Draw disjoint pie C#

I would like to 60 pie slices with of 5 degree and each with 1 degree of separation. However, with the code below, the outcome looks like the image below with a big black centre, is there anyway that I can modify the code to avoid that?
Graphics g = panel1.CreateGraphics();
// Create pen.
Pen blackPen = new Pen(Color.Black, 1);
// Create rectangle for ellipse.
//RectangleF rect = new RectangleF(150.0F, 10.0F, 200F, 300.0F);
// Create start and sweep angles.
float startAngle = 270F;
float sweepAngle = 4F;
for (int i = 0; i < 60; i++ )
{
RectangleF rect = new RectangleF(50F, 10.0F, 200F, 200F);
startAngle = startAngle + 6F;
g.DrawPie(blackPen, rect, startAngle, sweepAngle);
//start = start + 1f;
}
Regards
This effect is called the Moiré pattern. http://en.wikipedia.org/wiki/Moir%C3%A9_pattern
To avoid it make the distance between slices bigger. The amount depends on the size of the pie and the display resolution. To make the distance bigger you could draw a bigger pie but you'll find that the slices will be fighting over the pixels. You could just live with this effect or you could push the slices outward.

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

How to find which rectangle is closest to a point

If I have two System.Drawing.Rectangle objects on a canvas and a Point, what is the best way to calculate which Rectangle (any part of the Rectangle, not just its Location Point) is closest to that Point?
An example from a unit test:
Rectangle one = new Rectangle (0, 0, 10, 10);
Rectangle two = new Rectangle (20, 20, 10, 10);
Point point = new Point(14, 14);
Rectangle actual = ClosestToPoint(point, one, two);
// should be closer to one since one's bottom right is at (10, 10)
Assert.That(actual, Is.SameAs(one));
// method to write
public Rectangle ClosestToPoint(Point p, params Rectangle[] rectangles) { }
distance to rectangle = min (distance
to each of the 4 line segments that
are the edges of the rectangle)
For distance to line segment, see this question
This is for measuring distance between 2 points, so take the coordinate point from your rectangle (which is up to you to decide be cause I dont know what closest means for you):
public int Distance2D(int x1, int y1, int x2, int y2)
{
int result = 0;
double part1 = Math.Pow((x2 - x1), 2);
double part2 = Math.Pow((y2 - y1), 2);
double underRadical = part1 + part2;
result = (int)Math.Sqrt(underRadical);
return result;
}
Hmm. I'm thinking of looping through your rectangle array and storing each X and Y index in a list. You could then loops through the list and do something abs(min(i.getX() - point)). If they are equal then check for min(y).

Categories

Resources