Uz Galaxy - Info Archive top BBS   RSS
Info Archive  Graphics - 座標軸の設定  

既定の軸は 左上が原点で 右に +X軸、下に +Y軸 となってますが、画面の中央を原点にし、
右に +X軸、上に +Y軸と設定することができる。

        ' 原点を中央へ
        g.TranslateTransform(PictureBox2.Width / 2, PictureBox2.Height / 2)
        ' Y軸反転
        g.ScaleTransform(1, -1)



    Private Sub DrawGrid(ByVal g As Graphics)
        Dim x, y As Long
        Dim r As RectangleF
        Dim p As Pen = New Pen(Color.LightGray)

        r = g.VisibleClipBounds
        For x = 0 To r.Right Step 10
            g.DrawLine(p, x, r.Top, x, r.Bottom)
        Next
        For x = 0 To r.Left Step -10
            g.DrawLine(p, x, r.Top, x, r.Bottom)
        Next
        For y = 0 To r.Bottom Step 10
            g.DrawLine(p, r.Left, y, r.Right, y)
        Next
        For y = 0 To r.Top Step -10
            g.DrawLine(p, r.Left, y, r.Right, y)
        Next

        p.Color = Color.Red
        g.DrawLine(p, 0, r.Top, 0, r.Bottom)
        g.DrawLine(p, r.Left, 0, r.Right, 0)
    End Sub

    Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
        Dim g As Graphics
        Dim p As Pen = New Pen(Color.Black, 4)

        g = e.Graphics
        DrawGrid(g)
        p.EndCap = Drawing2D.LineCap.ArrowAnchor
        g.DrawLine(p, 0, 0, 50, 50)
        g.DrawString("(50, 50)", Me.Font, New SolidBrush(Color.Black), 50, 50)
    End Sub

    Private Sub PictureBox2_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox2.Paint
        Dim g As Graphics
        Dim mat As Drawing2D.Matrix
        Dim p As Pen = New Pen(Color.Black, 4)

        g = e.Graphics
        g.TranslateTransform(PictureBox2.Width / 2, PictureBox2.Height / 2)
        g.ScaleTransform(1, -1)
        DrawGrid(g)

        p.EndCap = Drawing2D.LineCap.ArrowAnchor
        g.DrawLine(p, 0, 0, 50, 50)

        mat = g.Transform
        g.TranslateTransform(50, 50)
        g.ScaleTransform(1, -1)
        g.DrawString("(50, 50)", Me.Font, New SolidBrush(Color.Black), 0, 0)
        g.Transform = mat
    End Sub


Copyright © 2004 Uz. All rights reserved.
http://uzgalaxy.com/
このページはInfoArchiveで作成されています。