vb.net绘制三角形,等腰三角形,直角三角形——类库14

20 篇文章 1 订阅
17 篇文章 1 订阅

根据三边绘制三角形,请移步:.net/vb.net根据三边绘制三角形(余弦定理应用)_大Mod_abfun的博客-CSDN博客

我们发现,.net的GDI+并没有给我们提供三角形的绘制,但是为了简单画出三角形(普通三角形、等腰三角形,直角三角形)

是不是因为太简单而没有呢,不管他上代码:

这是我新建的一个类,叫基本图形

Imports System.Drawing

Public Class BasicGraphical
    Dim Image As Bitmap
    Dim g As Graphics

    Public Function CreateBmp(size As Size)
        Image = New Bitmap(size.Width, size.Height)
        g = Graphics.FromImage(Image)
    End Function
    Public Function Clear(g As Graphics)
        g.Clear(Color.Transparent)
    End Function

    Public Overridable Function Clear(g As Graphics, c As Color)
        g.Clear(c)
    End Function

    Public Overridable Function CreateBmp(size As Size, c As Color)
        Image = New Bitmap(size.Width, size.Height)
        g = Graphics.FromImage(Image)
        g.Clear(c)
    End Function

    Public Function _90AngleTriangle(width As Integer, height As Integer, wi As Single, c As Color, state As Integer)
        Dim AT As AngleTriangle
        AT = AnTr(width, height, state)
        Dim points(3) As Point
        points(0) = AT.p1
        points(1) = AT.p2
        points(2) = AT.point
        points(3) = AT.p1
        g.DrawLines(New Pen(c, wi), points)
        Return Image
    End Function

    Public Function AnTr(width As Integer, height As Integer, state As Integer) As AngleTriangle
        Dim a As AngleTriangle
        Select Case state
            Case 1
                a.p1 = New Point(0, height)
                a.p2 = New Point(width, 0)
                a.point = New Point(0, 0)
                Return a
            Case 2
                a.p1 = New Point(0, 0)
                a.p2 = New Point(width, height)
                a.point = New Point(width, 0)
                Return a
            Case 3
                a.p1 = New Point(0, 0)
                a.p2 = New Point(width, height)
                a.point = New Point(0, height)
                Return a
            Case 4
                a.p1 = New Point(width, 0)
                a.p2 = New Point(0, height)
                a.point = New Point(width, height)
                Return a
            Case Else
                Return a
        End Select

    End Function


    Public Structure AngleTriangle
        Public point As Point
        Public p1 As Point
        Public p2 As Point
        Public state As Integer
    End Structure



    Public Function Triangle(p1 As Point, p2 As Point, p3 As Point, width As Single, c As Color)
        Dim points(3) As Point
        points(0) = p1
        points(1) = p2
        points(2) = p3
        points(3) = p1
        g.DrawLines(New Pen(c, width), points)
    End Function

    Public Function IsoscelesTriangle(p1 As Point, p2 As Point, c As Color, width As Single, state As String) As Bitmap
        Dim lines As _IsoscelesTriangle
        lines = IsTr(p1, p2, state)
        Dim points(3) As Point
        points(0) = lines.TopPoint
        points(1) = lines.BottomPoint1
        points(2) = lines.BottomPoint2
        points(3) = lines.TopPoint
        g.DrawLines(New Pen(c, width), points)
        Return Image
    End Function

    Public Function IsTr(p1 As Point, p2 As Point, state As String) As _IsoscelesTriangle
        Dim p3 As Point
        Dim p4 As Point
        Dim width As Integer
        Dim height As Integer
        If p1.X < p2.X AndAlso p1.Y < p2.Y Then
            p3 = New Point(p2.X, p1.X)
            p4 = New Point(p1.X, p2.Y)
            width = p3.X - p1.X
            height = p4.Y - p1.Y
        ElseIf p1.X > p2.X AndAlso p1.Y < p2.Y Then
            p3 = New Point(p1.X, p2.Y)
            p4 = New Point(p2.X, p1.Y)
            width = p1.X - p4.X
            height = p2.Y - p4.Y
        ElseIf p1.X > p2.X AndAlso p1.Y > p2.Y Then
            p3 = New Point(p1.X, p2.X)
            p4 = New Point(p2.X, p1.Y)
            width = p3.X - p2.X
            height = p4.Y - p2.Y
        ElseIf p1.X < p2.X AndAlso p1.Y > p2.Y Then
            p3 = New Point(p2.X, p1.Y)
            p4 = New Point(p1.X, p2.Y)
            width = p2.X - p4.X
            height = p1.Y - p4.Y
        End If
        Dim i As _IsoscelesTriangle
        Select Case state
            Case "bottom"
                i.TopPoint = New Point(width / 2, 0)
                i.BottomPoint1 = New Point(0, height)
                i.BottomPoint2 = New Point(width, height)
                Return i
            Case "top"
                i.TopPoint = New Point(width / 2, height)
                i.BottomPoint1 = New Point(0, 0)
                i.BottomPoint2 = New Point(width, 0)
                Return i
            Case "left"
                i.TopPoint = New Point(width, height / 2)
                i.BottomPoint1 = New Point(0, 0)
                i.BottomPoint2 = New Point(0, height)
                Return i
            Case "right"
                i.TopPoint = New Point(0, height / 2)
                i.BottomPoint1 = New Point(width, 0)
                i.BottomPoint2 = New Point(width, height)
                Return i
            Case Else
                Return i
        End Select
    End Function

    Public Structure _IsoscelesTriangle
        Public TopPoint As Point
        Public BottomPoint1 As Point
        Public BottomPoint2 As Point
        Public state As String
    End Structure

    Public Overridable Function PointList(p1 As Point, p2 As Point) As Rectangle
        Dim p3 As Point
        Dim p4 As Point
        Dim width As Integer
        Dim height As Integer
        Dim LeftTop As Point

        If p1.X < p2.X AndAlso p1.Y < p2.Y Then
            p3 = New Point(p2.X, p1.X)
            p4 = New Point(p1.X, p2.Y)
            width = p3.X - p1.X
            height = p4.Y - p1.Y
            LeftTop = p1
        ElseIf p1.X > p2.X AndAlso p1.Y < p2.Y Then
            p3 = New Point(p1.X, p2.Y)
            p4 = New Point(p2.X, p1.Y)
            width = p1.X - p4.X
            height = p2.Y - p4.Y
            LeftTop = p4
        ElseIf p1.X > p2.X AndAlso p1.Y > p2.Y Then
            p3 = New Point(p1.X, p2.X)
            p4 = New Point(p2.X, p1.Y)
            width = p3.X - p2.X
            height = p4.Y - p2.Y
            LeftTop = p2
        ElseIf p1.X < p2.X AndAlso p1.Y > p2.Y Then
            p3 = New Point(p2.X, p1.Y)
            p4 = New Point(p1.X, p2.Y)
            width = p2.X - p4.X
            height = p1.Y - p4.Y
            LeftTop = p4
        End If
        Return New Rectangle(LeftTop, New Size(width, height))
    End Function

End Class

其中:pointlist函数还没有用到,可以先留着,

调用时注意:一定要先执行CreateBmp(New Drawing.Size(1000, 1000))

        BG.CreateBmp(New Drawing.Size(1000, 1000))
        BackgroundImage = BG._90AngleTriangle(100, 400, 3, Color.Blue, 4)
        BackgroundImage = BG.IsoscelesTriangle(New Point(0, 0), New Point(400, 200), Color.Red, 3, "bottom")

效果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值