VB.Net Tutorial/2D Graphics/Point PointF

Материал из VB Эксперт
Перейти к: навигация, поиск

Add first point and size to another point

<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class MainClass

  public Shared Sub Main
       Dim sz As New Size(12, 12)
       " Create a Point
       Dim pt As New Point(20, 20)
       " Add poitn and size and copy to point
       pt.X += sz.Width
       pt.Y += sz.Height
       Console.WriteLine("Addition :" + pt.ToString())
  End Sub

End class</source>

Addition :{X=32,Y=32}

Draw line using PointF structure

<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class DrawLineUsingPointFStructure

  public Shared Sub Main
       Application.Run(New Form1)
  End Sub

End class public class Form1

 Inherits System.Windows.Forms.Form
 Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
       Dim blackPen As New Pen(Color.Black, 4)
       " 
       " 
       Dim ptf1 As New PointF(20.0F, 20.0F)
       Dim ptf2 As New PointF(200.0F, 200.0F)
       e.Graphics.DrawLine(blackPen, ptf1, ptf2)
 End Sub
 Public Sub New()
  
   MyBase.New()
   Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
   Me.ClientSize = New System.Drawing.Size(292, 273)
   Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
 End Sub

End Class</source>

Draw line using Point structure

<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class DrawLineUsingPointStructure

  public Shared Sub Main
       Application.Run(New Form1)
  End Sub

End class public class Form1

 Inherits System.Windows.Forms.Form
 Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
       Dim blackPen As New Pen(Color.Black, 4)
       " 
       " 
       Dim pt1 As New Point(20, 20)
       Dim pt2 As New Point(20, 200)
       e.Graphics.DrawLine(blackPen, pt1, pt2)
 End Sub
 Public Sub New()
  
   MyBase.New()
   Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
   Me.ClientSize = New System.Drawing.Size(292, 273)
   Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
 End Sub

End Class</source>

Point.Ceiling

<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class MainClass

  public Shared Sub Main
       Dim pt1 As New PointF(30.6F, 30.8F)
       Dim pt4 As Point = Point.Ceiling(pt1)
       Console.WriteLine(pt4.ToString())
  End Sub

End class</source>

{X=31,Y=31}

PointF and PointDouble

<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Public Class Tester

   Public Shared Sub Main
       " ----- Original PointF version.
       Dim singlePoint As New PointF(1 / 17, Math.PI)
       Console.WriteLine("PointF: " & singlePoint.ToString())
       Console.WriteLine("X: " & singlePoint.X)
       " ----- New Point2D version.
       Dim doublePoint As New Point2D(1 / 17, Math.PI)
       Console.WriteLine("Point2D: " & doublePoint.Tostring())
       Console.WriteLine("X: " & doublePoint.X)
       
   End Sub

End Class Public Class Point2D

   Public X As Double
   Public Y As Double
   Public Sub New(ByVal xPoint As Double, _
         ByVal yPoint As Double)
       Me.X = xPoint
       Me.Y = yPoint
   End Sub
   Public Overrides Function Tostring() As String
       Return "{X=" & X & ",Y=" & Y & "}"
   End Function

End Class</source>

PointF: {X=0.05882353, Y=3.141593}
X: 0.05882353
Point2D: {X=0.0588235294117647,Y=3.14159265358979}
X: 0.0588235294117647

Point.Round

<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class MainClass

  public Shared Sub Main
       Dim pt1 As New PointF(30.6F, 30.8F)
       Dim pt4 As Point = Point.Round(pt1)
       Console.WriteLine(pt4.ToString())
  End Sub

End class</source>

{X=31,Y=31}

Point.Truncate

<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class MainClass

  public Shared Sub Main
       Dim pt1 As New PointF(30.6F, 30.8F)
       Dim pt4 As Point = Point.Truncate(pt1)
       Console.WriteLine(pt4.ToString())
  End Sub

End class</source>

{X=30,Y=30}

Subtract point and size

<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class MainClass

  public Shared Sub Main
       " Create a Size 
       Dim sz As New Size(12, 12)
       " Create a Point
       Dim pt As New Point(20, 20)
       " Subtract point and size
       pt.X -= sz.Width
       pt.Y -= sz.Height
       Console.WriteLine("Subtraction :" + pt.ToString())
  End Sub

End class</source>

Subtraction :{X=8,Y=8}

Use Point

<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class UsePoint

  public Shared Sub Main
       Application.Run(New Form1)
  End Sub

End class public class Form1

 Inherits System.Windows.Forms.Form
 Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
       Dim pt As New Point(50, 50)
       Dim newPoint As Point = Point.Empty
       newPoint.X = 100
       newPoint.Y = 200
       Dim g As Graphics = Graphics.FromHwnd(Me.Handle)
       Dim pn As New Pen(Color.Blue, 4)
       g.DrawLine(pn, pt, newPoint)
       pn.Dispose()
       g.Dispose()
 End Sub
 Public Sub New()
  
   MyBase.New()
   Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
   Me.ClientSize = New System.Drawing.Size(292, 273)
   Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
 End Sub

End Class</source>

Use PointF

<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class UsePointF

  public Shared Sub Main
       Application.Run(New Form1)
  End Sub

End class public class Form1

 Inherits System.Windows.Forms.Form
 Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
       Dim pt As New PointF(50.0F, 50.0F)
       Dim newPoint As PointF = PointF.Empty
       newPoint.X = 100.0F
       newPoint.Y = 200.0F
       Dim g As Graphics = Graphics.FromHwnd(Me.Handle)
       Dim pn As New Pen(Color.Blue, 4)
       g.DrawLine(pn, pt, newPoint)
       pn.Dispose()
       g.Dispose()
 End Sub
 Public Sub New()
  
   MyBase.New()
   Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
   Me.ClientSize = New System.Drawing.Size(292, 273)
   Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
 End Sub

End Class</source>