<%@ Page EnableSessionState="False" enableViewState="False"%>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Drawing2D" %>
<html><head>
<script language="VB" runat="server">
      Dim mMatrixLine
      Dim mLeftX% = 65, mBottomY% = 40

Sub Page_Load(sender As Object, e As EventArgs) 
   If Request.QueryString("ShowGraph") <> "" Then
      Call ShowGraph(Request.QueryString("Zmax"), 440, 220)
   End If
End Sub

Sub Show_Click(sender As Object, e As EventArgs) 
        Dim fun$ = Replace(txtFun.Text, "+", "~")
        Dim x1! = Math.Abs(Val(txtXmin.Text))
        txtXmin.Text = x1
        lblFun.Text = txtFun.Text : Image1.Visible = True
        Image1.ImageUrl = Request.Url.AbsolutePath & "?ShowGraph=" _
                 & fun & "&Zmax=" & txtXmin.Text
End Sub

Sub ShowGraph(z1!, W%, H%)
        Dim obj As Object = CreateObject("MSScriptControl.ScriptControl")
        Dim strFun$, ErrorMsg$, Zmax%
        Dim x, y, z, R, dY, kY As Single
        Const DEG! = Math.PI / 180.0!
        obj.Language = "VBScript"
        strFun = Replace(Request.QueryString("ShowGraph"), "~", "+")
        On Error Resume Next
        W -= 2 * mLeftX : H -= 2 * mBottomY
        obj.AddCode("Function R(z):" & strFun & ":End Function")
        If obj.Error.Description <> "" Then ErrorMsg = obj.Error.Description : GoTo ENTER_X
        Zmax = Int(z1 * 180 + 0.5)
        Dim YY(Zmax) As System.Drawing.PointF, i%
        Dim MaxY! = Single.MinValue, MinY! = Single.MaxValue, _
            MaxX! = Single.MinValue, MinX! = Single.MaxValue
        For i = 0 To Zmax
            z = i * DEG
          R = obj.Run("R", z)
          x = R * Math.Cos(z) : y = R * Math.Sin(z)
          MaxY = IIf(y > MaxY, y, MaxY) : MinY = IIf(y < MinY, y, MinY)
          MaxX = IIf(x > MaxX, x, MaxX) : MinX = IIf(x < MinX, x, MinX)
          YY(i).X = x : YY(i).Y = y
        Next
ENTER_X:
        Dim bmp As New Bitmap(W + 2 * mLeftX, H + 2 * mBottomY, Imaging.PixelFormat.Format16bppRgb565)
        Dim gr As Graphics = Graphics.FromImage(bmp)
        With gr
          .Clear(Color.White)
           If ErrorMsg <> "" Then
             Dim fnt As New Font("Arial", 12)
             Dim stringSize As New SizeF(gr.MeasureString("Syntax error: ", fnt))
             gr.DrawString("Syntax error:", fnt, Brushes.Black, 40, 50)
             gr.DrawString(ErrorMsg, fnt, Brushes.Red, 40 + stringSize.Width, 50)
             fnt.Dispose()
           Else
             Dim dX! = (MaxX - MinX) / (W - 1)
             If MaxY = MinY Then MinY = 0
             If MaxY > 0 Then dY = MaxY
             If MinY <= 0 Then dY -= MinY
             kY = H / dY
             x = IIf(MinX < 0 And MaxX > 0, -MinX, 0) / dX:    y = 0
             If MaxY >= 0 And MinY >= 0 Then y = dY
             If MaxY > 0 And MinY < 0 Then y = MaxY
             mMatrixLine = New Matrix(1.0F, 0.0F, 0.0F, -kY, mLeftX, mBottomY + y * kY)
            .Transform = mMatrixLine
             Call DrawGrid(gr, x, y, H, W, kY, MinX, MaxX, MaxY, MinY)
            .Transform = New Matrix(1.0F, 0.0F, 0.0F, -kY, mLeftX + x, mBottomY + y * kY)
             For i = 0 To Zmax:     YY(i).X /= dX:     Next
            .DrawPolygon(Pens.Blue, YY)
            .Transform = mMatrixLine
            .DrawLine(Pens.Black, 0, 0, W, 0)
            .DrawLine(Pens.Black, x, y, x, y - H / kY)
           End If
        End With
        Call BmpBack(bmp) : gr.Dispose() : bmp.Dispose()
End Sub

Sub DrawGrid(gr As Graphics, x!, y!, H%, W%, kY!, x1!, x2!, maxY!, minY!)
    Dim i% = 0, pa As New GraphicsPath()
    Dim fnt As New Font("Verdana", 7)
    Dim dX! = W / 4, dY! = H / (4 * kY), _
    numX1% = Int(x / dX), numX2% = Int((W - x) / dX), _
    numY1% = Int(y / dY), numY2% = Int((H - y * kY) / (dY * kY)), _
   dXX! = (x2 - x1) / 4
    With pa
        gr.Transform = New Matrix(1.0F, 0.0F, 0.0F, 1, mLeftX - 10, mBottomY + 5 + y * kY)
        For i = -numX1 To numX2
            .StartFigure() : .AddLine(x + i * dX, y, x + i * dX, y - H / kY)
            gr.DrawString(FormatNumber(IIf(numX1 = 0 And x = 0, x1, 0) + i * dXX, 2, , , TriState.False), _
                        fnt, Brushes.Black, x + i * dX, H - y * kY)
        Next
        gr.Transform = New Matrix(1.0F, 0.0F, 0.0F, 1, 0, mBottomY + y * kY - 6)
        For i = numY1 To -numY2 Step -1
            .StartFigure() : .AddLine(0, i * dY, W - 1, i * dY)
            gr.DrawString(Format((i * dY), "0.00E+00"), _
                                fnt, Brushes.Black, 0, -(i * dY * kY))
        Next
        .CloseFigure()
    End With
    With gr
        .Transform = New Matrix(1.0F, 0.0F, 0.0F, 1, 0, 0)
        .DrawRectangle(Pens.LightSteelBlue, mLeftX, mBottomY, W - 1, H)
         fnt = New Font("Verdana", 8)
       .DrawString("Ymin(Y最小):"  & Format(minY, "0.00E+00"), fnt, Brushes.Black, 20, 1)
       .DrawString("Ymax(Y最大):"  & Format(maxY, "0.00E+00"), fnt, Brushes.Black, W - 20, 1)
       .DrawString("Y(+)", fnt, Brushes.Black, mLeftX + x - 10, 23)
       .DrawString("X(+)", fnt, Brushes.Black, mLeftX + W, mBottomY + y * kY - 8)
       .Transform = mMatrixLine
       .DrawPath(Pens.LightSteelBlue, pa)
    End With
    pa.Dispose() : fnt.Dispose()
End Sub

Sub BmpBack(bmp As Bitmap)
   With Response
        .Clear() :              .ContentType = "image/Jpeg"
         bmp.Save(.OutputStream, Imaging.ImageFormat.Jpeg):        .End()
   End With
End Sub
</script><style>.txt { WIDTH: 100% }</style></head>


<body>
<form runat="server" ID="Form1">
<H3 align="center">数学图片计算器<br>
<FONT size="2">( R-Z&nbsp; 极坐标系统)</FONT></H3>
<TABLE style="FONT-SIZE: 8pt; FONT-FAMILY: Arial" width="550" align="center" border="0">
<TR>
<TD align="right" width="150">角度:&nbsp; Zmax(Z最大值)=</TD>
<TD align="right" width="150"><asp:textbox id="txtXmin" style="TEXT-ALIGN: right" runat="server" CssClass="txt">2</asp:textbox></TD>
<TD align="left" width="150">x</TD>
<TD align="left" width="150">PI=&nbsp; 3.1415926536</TD>
</TR>
<TR>
<TD vAlign="top" align="right">R(半径):</TD>
<TD colSpan="3"><asp:textbox id="txtFun" runat="server" CssClass="txt" TextMode="MultiLine">R = cos(4.0*z)</asp:textbox></TD>
</TR>
<TR>
<TD align="right">例如输入:</TD>
<TD align="left" colSpan="3"><nobr>一)&nbsp; R = 3*cos(z) - cos(3*z)<br>
二)&nbsp; For i = 0 To 5: p = p -&nbsp;z ^ i: Next:&nbsp;R = p</nobr></TD>
</TR>
<TR><TD></TD><TD></TD><TD></TD>
<TD align="right"><asp:button runat="server" Text="生成图片" onclick="Show_Click" ID="Button1"></asp:button></TD>
</TR>
<TR>
<TD align="middle" colSpan="4"><b>自动图片:</b>
<asp:label id="lblFun" runat="server" ForeColor="#0000C0" Font-Bold="True">Y=...</asp:label><br>
<asp:image id="Image1" style="BORDER: #0000c0 1px solid;" 
         runat="server" Width="440px" Height="220px" Visible="False"></asp:image></TD>
</TR>
</TABLE>
</form></body></html>
