Hello.
Im trying to draw objects (in this case lines) that have same geometry. Since they would appear on top of each other i have found that using CompoundArray property i can visualize the offset as desired. The code is just a test code, so if some one wants to have a look let me know. Can some1 explain why im getting this weird glitch in the inner corners of lines that have CompoundArray applied? Any suggestion would be appreciated.
Here is a simple "test" class for this problem:
public partial class Form1 : Form
{
int penWidth;
int penOffset;
Color penColor;
public Form1()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Point[] linePoints = this.LinePoints();
GraphicsPath path1 = new GraphicsPath();
path1.AddLines(linePoints);
// red line no offset
this.penWidth = 10;
this.penOffset = 0;
this.penColor = Color.Red;
Pen linePen1 = this.CreatePen(penWidth, penOffset, penColor);
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.DrawPath(linePen1, path1);
// blue line offset 10
this.penWidth = 10;
this.penOffset = 10;
this.penColor = Color.Blue;
Pen linePen2 = this.CreatePen(penWidth, penOffset, penColor);
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.DrawPath(linePen2, path1);
// green line offset -30
this.penWidth = 1;
this.penOffset = -30;
this.penColor = Color.Green;
Pen linePen3 = this.CreatePen(penWidth, penOffset, penColor);
linePen3.MiterLimit = 2;
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.DrawPath(linePen3, path1);
// black rectangle no offset
this.penWidth = 10;
this.penOffset = 0;
this.penColor = Color.Black;
Pen rectanglePen = this.CreatePen(penWidth, penOffset, penColor);
Rectangle rect = new Rectangle(600, 200, 100, 100);
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.DrawRectangle(rectanglePen, rect);
// blue rectangle offset 10
this.penWidth = 10;
this.penOffset = 10;
this.penColor = Color.Blue;
Pen rectanglePen1 = this.CreatePen(penWidth, penOffset, penColor);
Rectangle rect1 = new Rectangle(600, 200, 100, 100);
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.DrawRectangle(rectanglePen1, rect1);
// teal rectangle offset 50
this.penWidth = 10;
this.penOffset = 50;
this.penColor = Color.Teal;
Pen rectanglePen4 = this.CreatePen(penWidth, penOffset, penColor);
Rectangle rect4 = new Rectangle(600, 200, 100, 100);
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.DrawRectangle(rectanglePen4, rect4);
// violet rectangle offset -10
this.penWidth = 10;
this.penOffset = -10;
this.penColor = Color.Violet;
Pen rectanglePen2 = this.CreatePen(penWidth, penOffset, penColor);
Rectangle rect2 = new Rectangle(600, 200, 100, 100);
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.DrawRectangle(rectanglePen2, rect2);
// tomato rectangle offset -30
this.penWidth = 10;
this.penOffset = -30;
this.penColor = Color.Tomato;
Pen rectanglePen3 = this.CreatePen(penWidth, penOffset, penColor);
Rectangle rect3 = new Rectangle(600, 200, 100, 100);
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.DrawRectangle(rectanglePen3, rect3);
}
public Pen CreatePen (int width, int offset, Color color)
{
Pen pen = new Pen(color, width);
if (offset == 0)
{
return pen;
}
else
{
return (this.ModifyPenWithOffset(pen, offset));
}
}
public Pen ModifyPenWithOffset(Pen pen, int offset)
{
var originalPenWidth = pen.Width;
pen.Width = 2 * (pen.Width + Math.Abs(offset));
if (offset > 0)
{
var calculation = originalPenWidth / pen.Width;
pen.CompoundArray = new Single[] { 0.0F, calculation };
return pen;
}
else
{
var calculation = (pen.Width - originalPenWidth) / pen.Width;
pen.CompoundArray = new Single[] { calculation, 1.0F };
return pen;
}
}
public Point[] LinePoints()
{
Point[] points = new Point[12];
points[0] = new Point(0, 0);
points[1] = new Point(50, 100);
points[2] = new Point(100, 220);
points[3] = new Point(60, 300);
points[4] = new Point(40, 330);
points[5] = new Point(60, 360);
points[6] = new Point(180, 200);
points[7] = new Point(200, 60);
points[8] = new Point(270, 60);
points[9] = new Point(270, 200);
points[10] = new Point(350, 200);
points[11] = new Point(350, 150);
return points;
}
}