<%@ Page EnableSessionState="False" enableViewState="False" CodeBehind="Fun3d_1.aspx.cs" Language="c#" AutoEventWireup="false" Inherits="WebOCRM.Tools.Fun3d_1" %>
<%@ 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("Xmin"), Request.QueryString("Xmax"), 440, 220)
   End If
End Sub

Sub Show_Click(sender As Object, e As EventArgs) 
        Dim fun$ = Replace(txtFun.Text, "+", "~")
        Dim x1! = Val(txtXmin.Text), x2! = Val(txtXmax.Text), x! = 0
        If x1 > x2 Then
            x = x1 :           x1 = x2 :          x2 = x
        End If
        txtXmin.Text = x1 :                   txtXmax.Text = x2
        lblFun.Text = txtFun.Text:        Image1.Visible = True
        Image1.ImageUrl = Request.Url.AbsolutePath & "?ShowGraph=" _
                 & fun & "&Xmin=" & txtXmin.Text & "&Xmax=" & txtXmax.Text
End Sub

Sub ShowGraph(x1!, x2!, W%, H%)
        Dim obj As Object = CreateObject("MSScriptControl.ScriptControl")
        Dim strFun$, ErrorMsg$
        Dim x, y, dY, kY As Single
        obj.Language = "VBScript"
        strFun = Replace(Request.QueryString("ShowGraph"), "~", "+")
        On Error Resume Next
        W -= 2 * mLeftX :              H -= 2 * mBottomY
        obj.AddCode("Function Y(x):" & strFun & ":End Function")
        If obj.Error.Description <> "" Then ErrorMsg = obj.Error.Description : GoTo ENTER_X
        Dim MaxY! = Single.MinValue, MinY! = Single.MaxValue, _
            dX! = (x2 - x1) / (W - 1), i% = 0
        Dim YY(W + 1) As System.Drawing.PointF
        For i = 0 To W - 1
            x = x1 + dX * i :                 y = 0
            y = obj.Run("Y", x)
            MaxY = IIf(y > MaxY, y, MaxY) : MinY = IIf(y < MinY, y, MinY)
            YY(i + 1).X = i :                YY(i + 1).Y = y
        Next
        YY(W + 1).X = W - 1
  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
             If MaxY = MinY Then MinY = 0
             If MaxY > 0 Then dY = MaxY
             If MinY <= 0 Then dY -= MinY
             kY = H / dY:             x = IIf(x1 < 0 And x2 > 0, -x1, 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
            .FillPolygon(Brushes.LightYellow, YY, Drawing.Drawing2D.FillMode.Alternate)
             Call DrawGrid(gr, x, y, H, W, kY, x1, x2, MaxY!, MinY!)
            .DrawPolygon(Pens.Blue, YY)
            .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, 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">( 函数方程式系统)</FONT></H3>
     <TABLE style="FONT-SIZE: 8pt; FONT-FAMILY: Arial" width="440" align="center" border="0">
          <TR>
                  <TD WIDTH=110 align="right">Xmin(X最小值)=</TD>
	<TD WIDTH=110  align="right">
                       <asp:textbox id="txtXmin" runat="server" CssClass="txt">-5</asp:textbox></TD>
	<TD WIDTH=110 align="right">Xmax(X最大值)=</TD>
	<TD WIDTH=110 align="right">
                       <asp:textbox id="txtXmax" runat="server" CssClass="txt">5</asp:textbox></TD>
          </TR>
          <TR>
	<TD vAlign="top" align="right">方程式:</TD>
	<TD colSpan="3">
                        <asp:textbox id="txtFun" runat="server" CssClass="txt" TextMode="MultiLine">Y = x/2. + sin(x)</asp:textbox></TD>
          </TR>
          <TR>
	<TD align="right">例如输入:</TD>
	<TD align="left" colSpan="3"><nobr>
                                  一)&nbsp; Y=10*x + 5/x - 40*sin(x)<br>
	                二)&nbsp; For i = 0 To 5: p = p + x ^ i: Next: Y = 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"><STRONG>Graph:</STRONG>
	          <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>
