Csharp/CSharp Tutorial/2D/Point
Содержание
Add point and size and copy to point
<source lang="csharp">using System; using System.Drawing; using System.Windows.Forms; class PaintHello {
public static void Main() { Size sz = new Size(12, 12); Point pt = new Point(20, 20); pt = pt+sz; MessageBox.Show("Addition :"+ pt.ToString()); pt = pt-sz; MessageBox.Show("Subtraction :"+ pt.ToString()); PointF ptf = pt; MessageBox.Show("PointF :"+ pt.ToString()); sz = (Size)pt; MessageBox.Show("Size :"+ sz.Width.ToString() +","+ sz.Height.ToString() ); }
}</source>
Concert Point to Size
<source lang="csharp">using System; using System.Drawing; using System.Windows.Forms; class PaintHello {
public static void Main() { Size sz = new Size(12, 12); Point pt = new Point(20, 20); pt = pt+sz; MessageBox.Show("Addition :"+ pt.ToString()); pt = pt-sz; MessageBox.Show("Subtraction :"+ pt.ToString()); PointF ptf = pt; MessageBox.Show("PointF :"+ pt.ToString()); sz = (Size)pt; MessageBox.Show("Size :"+ sz.Width.ToString() +","+ sz.Height.ToString() ); }
}</source>
Create and offset a point
<source lang="csharp">using System; using System.Drawing; class MainClass {
public static int Main(string[] args) { Point pt = new Point(100, 72); Console.WriteLine(pt); pt.Offset(20, 20); Console.WriteLine(pt); return 0; }
}</source>
{X=100,Y=72} {X=120,Y=92}
Create a PointF from Point
<source lang="csharp">using System; using System.Drawing; using System.Windows.Forms; class PaintHello {
public static void Main() { Size sz = new Size(12, 12); Point pt = new Point(20, 20); pt = pt+sz; MessageBox.Show("Addition :"+ pt.ToString()); pt = pt-sz; MessageBox.Show("Subtraction :"+ pt.ToString()); PointF ptf = pt; MessageBox.Show("PointF :"+ pt.ToString()); sz = (Size)pt; MessageBox.Show("Size :"+ sz.Width.ToString() +","+ sz.Height.ToString() ); }
}</source>
Createing Point
<source lang="csharp">using System; using System.Drawing; using System.Windows.Forms; class PaintHello {
public static void Main() { Form form = new Form(); form.BackColor = Color.White; form.Paint += new PaintEventHandler(MyPaintHandler); Application.Run(form); } static void MyPaintHandler(object objSender, PaintEventArgs pea) { Form form = (Form)objSender; Graphics g = pea.Graphics; Point pt = new Point(50, 50); Point newPoint = Point.Empty; newPoint.X = 100; newPoint.Y = 200; Pen pn = new Pen(Color.Blue, 4); g.DrawLine(pn, pt, newPoint); pn.Dispose(); g.Dispose(); }
}</source>
Createing PointF
<source lang="csharp">using System; using System.Drawing; using System.Windows.Forms; class PaintHello {
public static void Main() { Form form = new Form(); form.BackColor = Color.White; form.Paint += new PaintEventHandler(MyPaintHandler); Application.Run(form); } static void MyPaintHandler(object objSender, PaintEventArgs pea) { Form form = (Form)objSender; Graphics g = pea.Graphics; PointF pt = new PointF(50.0F, 50.0F); PointF newPoint = PointF.Empty; newPoint.X = 100.0F; newPoint.Y = 200.0F; Pen pn = new Pen(Color.Blue, 4); g.DrawLine(pn, pt, newPoint); pn.Dispose(); g.Dispose();
}
}</source>
Overloaded Point operators: ==
<source lang="csharp">using System; using System.Drawing; class MainClass {
public static int Main(string[] args) { Point pt = new Point(100, 72); Console.WriteLine(pt); Point pt2 = pt; if(pt == pt2) Console.WriteLine("Points are the same"); else Console.WriteLine("Different points"); return 0; }
}</source>
{X=100,Y=72} Points are the same
Point Ceiling, Round, Truncate
<source lang="csharp">using System; using System.Drawing; using System.Windows.Forms; class PaintHello {
public static void Main() { PointF pt1 = new PointF(30.6f, 30.8f); PointF pt2 = new PointF(50.3f, 60.7f); PointF pt3 = new PointF(110.3f, 80.5f); Point pt4 = Point.Ceiling(pt1); Point pt5 = Point.Round(pt2); Point pt6 = Point.Truncate(pt3); MessageBox.Show("Value of pt4: " +pt4.ToString()); MessageBox.Show("Value of pt5: " +pt5.ToString()); MessageBox.Show("Value of pt6: " +pt6.ToString());
}
}</source>
Subtract point and size
<source lang="csharp">using System; using System.Drawing; using System.Windows.Forms; class PaintHello {
public static void Main() { Size sz = new Size(12, 12); Point pt = new Point(20, 20); pt = pt+sz; MessageBox.Show("Addition :"+ pt.ToString()); pt = pt-sz; MessageBox.Show("Subtraction :"+ pt.ToString()); PointF ptf = pt; MessageBox.Show("PointF :"+ pt.ToString()); sz = (Size)pt; MessageBox.Show("Size :"+ sz.Width.ToString() +","+ sz.Height.ToString() ); }
}</source>