Csharp/CSharp Tutorial/2D/Point

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Add point and size and copy to point

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() );
     }
}

Concert Point to Size

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() );
     }
}

Create and offset a point

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;
  }
}
{X=100,Y=72}
{X=120,Y=92}

Create a PointF from Point

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() );
     }
}

Createing Point

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();
     }
}

Createing PointF

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();        

     }
}

Overloaded Point operators: ==

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;
  }
}
{X=100,Y=72}
Points are the same

Point Ceiling, Round, Truncate

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());

     }
}

Subtract point and size

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() );
     }
}