Jump to content

plotting tool


imbatmo
 Share

Recommended Posts

i have written this so far:

; includes / options
#include <GUIConstants.au3>
#include <Constants.au3>
Opt("GUIOnEventMode", 1)
Opt("GUICloseOnESC", 0)

;declare Variables
Global $red = "0xff0000"
Global $green = "0x006633"
Global $blue = "0x0000ff"
Global $desktopx = 600
Global $desktopy = 600

;create GUI and GUICtrl's
$gui = GUICreate("tmo's Plotting Tool", 350, 150)

$menu = GUICtrlCreateMenu("File")
$subclose = GUICtrlCreateMenuitem("Exit", $menu)
$menu2 = GUICtrlCreateMenu("Help")
$subcredits = GUICtrlCreateMenuitem("Credits", $menu2)

$check_f   = GUICtrlCreateCheckbox("", 10, 12)
$label_f   = GUICtrlCreateLabel("f(x) = ", 30, 12, 30, 20)
$input_f   = GUICtrlCreateInput("", 60, 10, 150, 20)
$preview_f = GUICtrlCreateLabel("", 220, 10, 20, 20)
GUICtrlSetBkColor($preview_f, $blue)
$color_f   = GUICtrlCreateCombo("Blue", 250, 10, 60, 20)
GUICtrlSetData($color_f, "Green|Red")

$check_g   = GUICtrlCreateCheckbox("", 10, 40)
$label_g   = GUICtrlCreateLabel("g(x) = ", 30, 42, 30, 20)
$input_g   = GUICtrlCreateInput("", 60, 40, 150, 20)
$preview_g = GUICtrlCreateLabel("", 220, 40, 20, 20)
GUICtrlSetBkColor($preview_g, $red)
$color_g   = GUICtrlCreateCombo("Red", 250, 40, 60, 20)
GUICtrlSetData($color_g, "Blue|Green")

$check_h   = GUICtrlCreateCheckbox("", 10, 68)
$label_h   = GUICtrlCreateLabel("h(x) = ", 30, 72, 30, 20)
$input_h   = GUICtrlCreateInput("", 60, 70, 150, 20)
$preview_h = GUICtrlCreateLabel("", 220, 70, 20, 20)
GUICtrlSetBkColor($preview_h, $green)
$color_h   = GUICtrlCreateCombo("Green", 250, 70, 60, 20)
GUICtrlSetData($color_h, "Red|Blue")

$button = GUICtrlCreateButton("Draw!", 150, 100, 50, 20)

; set events
GUICtrlSetOnEvent($subcredits, "_credits")
GUICtrlSetOnEvent($subclose, "_exit")
GUICtrlSetOnEvent($button, "_start")
GUICtrlSetOnEvent($color_f, "_colorF")
GUICtrlSetOnEvent($color_g, "_colorG")
GUICtrlSetOnEvent($color_h, "_colorH")
GUISetOnEvent($GUI_EVENT_CLOSE, "_exit")

;show GUI
GUISetState()

While 1
    Sleep(0)
WEnd

Func _colorF()
    $newcolor = GUICtrlRead($color_f)
    If $newcolor = "Blue" Then 
        GUICtrlSetBkColor($preview_f, $blue)
    ElseIf $newcolor = "Red" Then 
        GUICtrlSetBkColor($preview_f, $red)
    Else 
        GUICtrlSetBkColor($preview_f, $green)
    EndIf
EndFunc
    
Func _colorG()
    $newcolor = GUICtrlRead($color_g)
    If $newcolor = "Blue" Then 
        GUICtrlSetBkColor($preview_g, $blue)
    ElseIf $newcolor = "Red" Then 
        GUICtrlSetBkColor($preview_g, $red)
    Else 
        GUICtrlSetBkColor($preview_g, $green)
    EndIf
EndFunc
    
Func _colorH()
    $newcolor = GUICtrlRead($color_h)
    If $newcolor = "Blue" Then 
        GUICtrlSetBkColor($preview_h, $blue)
    ElseIf $newcolor = "Red" Then 
        GUICtrlSetBkColor($preview_h, $red)
    Else 
        GUICtrlSetBkColor($preview_h, $green)
    EndIf
EndFunc

;start drawing
Func _start()
    HotKeySet("{ESC}", "_closeDraw")
    $draw_gui = GUICreate("Press ESC to close", $desktopx, $desktopy)
    GUISetBkColor("0xffffff")
    $graphic = GUICtrlCreateGraphic(0, 0, $desktopx, $desktopy)
    For $i = -10 To 10
        If $i = 0 Then
            GUICtrlSetGraphic(-1, $GUI_GR_COLOR, "0x000000")
        Else
            GUICtrlSetGraphic(-1, $GUI_GR_COLOR, "0x999999")
        EndIf
        GUICtrlSetGraphic(-1, $GUI_GR_MOVE, $i * $desktopx / 20 + $desktopx / 2, 0)
        GUICtrlSetGraphic(-1, $GUI_GR_LINE, $i * $desktopx / 20 + $desktopx / 2, $desktopy)
    Next
    For $i = -10 To 10
        If $i = 0 Then
            GUICtrlSetGraphic(-1, $GUI_GR_COLOR, "0x000000")
        Else
            GUICtrlSetGraphic(-1, $GUI_GR_COLOR, "0x999999")
        EndIf
        GUICtrlSetGraphic(-1, $GUI_GR_MOVE, 0, $i * $desktopy / 20 + $desktopy / 2)
        GUICtrlSetGraphic(-1, $GUI_GR_LINE, $desktopx, $i * $desktopy / 20 + $desktopy / 2)
    Next
    
    ;draw f(x)
    If GUICtrlRead($check_f) = $GUI_CHECKED Then
        $newcolor = GUICtrlRead($color_f)
        If $newcolor = "Blue" Then 
            $color = $blue
        ElseIf $newcolor = "Red" Then 
            $color = $red
        Else 
            $color = $green
        EndIf
        GUICtrlSetGraphic(-1, $GUI_GR_COLOR, $color)
        $eval = GUICtrlRead($input_f)
            $eval = StringReplace($eval, "x", "$x")
        $i = 0
        $pixelx = $i
        $x = ($i - 300) / 30
        $y = Execute($eval)
        $pixely = -1*$y*30 + 300
        For $i = 1 To 600
            GUICtrlSetGraphic(-1, $GUI_GR_MOVE, $pixelx, $pixely)
            $pixelx = $i
            $x = ($i - 300) / 30
            $y = Execute($eval)
            $pixely = -1*$y*30 + 300
            If $y <> 1/0 Then GUICtrlSetGraphic(-1, $GUI_GR_LINE, $pixelx, $pixely)
        Next
    EndIf
    ;draw g(x)
    If GUICtrlRead($check_g) = $GUI_CHECKED Then
        $newcolor = GUICtrlRead($color_g)
        If $newcolor = "Blue" Then 
            $color = $blue
        ElseIf $newcolor = "Red" Then 
            $color = $red
        Else 
            $color = $green
        EndIf
        GUICtrlSetGraphic(-1, $GUI_GR_COLOR, $color)
        $eval = GUICtrlRead($input_g)
            $eval = StringReplace($eval, "x", "$x")
        $i = 0
        $pixelx = $i
        $x = ($i - 300) / 30
        $y = Execute($eval)
        $pixely = -1*$y*30 + 300
        For $i = 1 To 600
            GUICtrlSetGraphic(-1, $GUI_GR_MOVE, $pixelx, $pixely)
            $pixelx = $i
            $x = ($i - 300) / 30
            $y = Execute($eval)
            $pixely = -1*$y*30 + 300
            If $y <> 1/0 Then GUICtrlSetGraphic(-1, $GUI_GR_LINE, $pixelx, $pixely)
        Next
    EndIf
    ;draw h(x)
    If GUICtrlRead($check_h) = $GUI_CHECKED Then
        $newcolor = GUICtrlRead($color_h)
        If $newcolor = "Blue" Then 
            $color = $blue
        ElseIf $newcolor = "Red" Then 
            $color = $red
        Else 
            $color = $green
        EndIf
        GUICtrlSetGraphic(-1, $GUI_GR_COLOR, $color)
        $eval = GUICtrlRead($input_h)
            $eval = StringReplace($eval, "x", "$x")
        $i = 0
        $pixelx = $i
        $x = ($i - 300) / 30
        $y = Execute($eval)
        $pixely = -1*$y*30 + 300
        For $i = 1 To 600
            GUICtrlSetGraphic(-1, $GUI_GR_MOVE, $pixelx, $pixely)
            $pixelx = $i
            $x = ($i - 300) / 30
            $y = Execute($eval)
            $pixely = -1*$y*30 + 300
            If $y <> 1/0 Then GUICtrlSetGraphic(-1, $GUI_GR_LINE, $pixelx, $pixely)
        Next
    EndIf
    GUISetState()
EndFunc

;exit the script
Func _exit()
    Exit
EndFunc

Func _closeDraw()
    HotKeySet("{ESC}")
    GUIDelete()
EndFunc

;credits
Func _credits()
    MsgBox(0, "Credits", "Written by: tmo")
EndFunc

test it with something like 3*$x^3 - 4*$x^2 - 2*$x...

notice that x has to be typed as $x and u cant use something like 2$x. always type the *, e.g. 2*$x

if anyone feels like improving this, feel free to do it :)

Edited by imbatmo
Link to comment
Share on other sites

VERY nice!

Its like a graphing Calculator

but can you use the 'y' variable too? so I can make 2 "perpendicular" parabolas?

also, is it possible to expand it to have one more line? so it can plot 4 lines instead of just 3?

Very nice though, and i would try to do these things themselves, but the fact that you made this shows that you are WAY better than me at math

Mind if I PM you for math help?

Link to comment
Share on other sites

test it with something like 3*$x^3 - 4*$x^2 - 2*$x...

notice that x has to be typed as $x and u cant use something like 2$x. always type the *, e.g. 2*$x

if anyone feels like improving this, feel free to do it :D

Nice job!!! :)

2 little improvements you can do:

-Use StringReplace to replace x by $x. In this way you can write formulas using letters as variables only...

-Adjust the size of the window to the size of the desktop (I have to change my resolution to see the entire window)...

Would be nice if you can export the graphic to a file (f.e. a gif file)

I'll work on this and post later...

:P

Link to comment
Share on other sites

of course i could make it with more than 3 functions.

made some other improvement for functions like 1/(x-2) (in this case there were problems for x = 2 of course)

just changed

GUICtrlSetGraphic(-1, $GUI_GR_LINE, $pixelx, $pixely)

to

If $y <> 1/0 Then GUICtrlSetGraphic(-1, $GUI_GR_LINE, $pixelx, $pixely)

because autoit doesnt end in a error when dividing by zero, it justs returns 1.#INF so this works

should work like this way with functions like x^0.5 (doesnt work for x<0). i will do this tomorrow i think (hope :P)

edit: StringReplace()-thingie done :)

Edited by imbatmo
Link to comment
Share on other sites

This is really good. I'm glad the sin, cos and tan functions work with execute :).

Try

Sin(x)+Cos(x)+tan(x)

edit: woah cpu is at ~97 with this. Change the sleep(0) to sleep(1000)

Edited by RazerM
My Programs:AInstall - Create a standalone installer for your programUnit Converter - Converts Length, Area, Volume, Weight, Temperature and Pressure to different unitsBinary Clock - Hours, minutes and seconds have 10 columns each to display timeAutoIt Editor - Code Editor with Syntax Highlighting.Laserix Editor & Player - Create, Edit and Play Laserix LevelsLyric Syncer - Create and use Synchronised Lyrics.Connect 4 - 2 Player Connect 4 Game (Local or Online!, Formatted Chat!!)MD5, SHA-1, SHA-256, Tiger and Whirlpool Hash Finder - Dictionary and Brute Force FindCool Text Client - Create Rendered ImageMy UDF's:GUI Enhance - Enhance your GUIs visually.IDEA File Encryption - Encrypt and decrypt files easily! File Rename - Rename files easilyRC4 Text Encryption - Encrypt text using the RC4 AlgorithmPrime Number - Check if a number is primeString Remove - remove lots of strings at onceProgress Bar - made easySound UDF - Play, Pause, Resume, Seek and Stop.
Link to comment
Share on other sites

Also here's some good things to implement:

- Boundaries, like: for x = -250 to 250

- The ability to use the outcame of a formule in a different formule. Say f(x) = 0.5*x and g(x) = f*x

How about another variable? like x and y?

How do you get Pi?

Try this one

f(x)=Sin(x)

g(x)=-3*Cos(x)

h(x)=3*Cos(x)

Edited by Paulie
Link to comment
Share on other sites

of course i could make it with more than 3 functions.

made some other improvement for functions like 1/(x-2) (in this case there were problems for x = 2 of course)

just changed

GUICtrlSetGraphic(-1, $GUI_GR_LINE, $pixelx, $pixely)

to

If $y <> 1/0 Then GUICtrlSetGraphic(-1, $GUI_GR_LINE, $pixelx, $pixely)

because autoit doesnt end in a error when dividing by zero, it justs returns 1.#INF so this works

should work like this way with functions like x^0.5 (doesnt work for x<0). i will do this tomorrow i think (hope :D)

edit: StringReplace()-thingie done :P

4 little things to improve... :D

1)display functions in the graph window (near a corner)

2)zoom in or out

3)display scale

4)saving as .gif

I'm working in... post in a few days...

Suggestions wellcome...

Thanks imbatmo for the code and the idea... :)

I think this program would be very usefull in teaching!!! don´t you?

Link to comment
Share on other sites

  • 1 month later...

This is really nice!

A simple improvement in the code would be to make your drawing function accept an array of coefficients, and then you could easily use horners method to compute the y values. It would be a better design for your code. Then you could easily write a helper function or two for better error checking.

BTW horners method is faster (though I doubt it speed really matters.)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...