1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace TestPenInset
{
public partial class FormWidenPath : Form
{
public FormWidenPath()
{
InitializeComponent();
}
private void FormWidenPath_Paint(object sender, PaintEventArgs e)
{
Graphics gr = e.Graphics;
Rectangle r= new Rectangle(50, 50, 300, 150);//rectangle
List<PointF> pts = GetPoints(); //polygone
using (GraphicsPath path =new GraphicsPath())
{
using (Pen penRed = new Pen(Color.Red, 1.0f))
{
//rectangle ligne theorique
r = new Rectangle(50, 50, 300, 150);
path.AddRectangle(r);
gr.DrawPath(penRed, path);
//polygon ligne theorique
path.Reset();
path.AddPolygon(pts.ToArray());
gr.DrawPath(penRed, path);
}
using (Pen penWiden = new Pen(Color.Transparent, 20.0f))
{
using (Pen penBlue = new Pen(Color.Blue, 1.0f))
{
//rectangle avec decalage de 20.0f
path.Reset();
path.AddRectangle(r);
path.Widen(penWiden);
gr.DrawPath(penBlue, path);
using (SolidBrush fill = new SolidBrush(Color.FromArgb(100, Color.Tomato)))
{
gr.FillPath(fill, path);
}
//polygon avec decalage de 20.0f
path.Reset();
path.AddPolygon(pts.ToArray());
path.Widen(penWiden);
gr.DrawPath(penBlue, path);
using (SolidBrush fill = new SolidBrush(Color.FromArgb(100, Color.Tomato)))
{
gr.FillPath(fill, path);
}
}
}
}
}
//pentagone (figure à 5 cotes)
private List<PointF> GetPoints()
{
float radius = 100.0f;
PointF center = new PointF(400, 400);
double theta = 2*Math.PI / 5.0;
double angle = 0.0;
List<PointF> l = new List<PointF>();
for (int i = 0; i < 5; i++)
{
float x = center.X + radius * (float)Math.Cos(angle);
float y = center.Y + radius * (float)Math.Sin(angle);
PointF p = new PointF(x, y);
l.Add(p);
angle += theta;
}
return l;
}
}
} |
Partager