File: graph2d.py

package info (click to toggle)
ocempgui 0.2.8-1.1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 4,464 kB
  • ctags: 1,849
  • sloc: python: 9,304; ansic: 6,849; makefile: 179
file content (59 lines) | stat: -rw-r--r-- 1,540 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# Graph2D examples.
from ocempgui.widgets import *
from ocempgui.widgets.Constants import *
import Numeric, math

__function = "func_1"

def change(graph):
    global __function
    if __function == "func_1":
        graph.eval_func = lambda x: x / ((- 3 * x**2.0 + 2) * math.e**x)
        __function = "func_2"
    else:
        graph.eval_func = lambda x: x**4.0 - 3 * x**2.0 + 2 * x
        __function = "func_1"
    
def create_graph2d_view ():
    frame = VFrame (Label ("Graph2D"))
    frame.topleft = 10, 10

    # Create the graph.
    graph = Graph2D (400, 400)

    # Lock it, because we set some necessary information and want to
    # avoid excessive update() calls.
    graph.lock ()

    # Scale units for the x and y axis.
    graph.scale_units = ("cm", "kp")

    # Point of origin.
    graph.origin = 200, 200

    # We want to see negative values.
    graph.negative = True

    # The evaluation function and data to use.
    graph.eval_func = lambda x: x**4.0 - 3 * x**2.0 + 2 * x
    graph.data = Numeric.arrayrange (-10, 10, .001).tolist()

    # Done, unlock.
    graph.unlock ()

    button = Button ("Change function")
    button.connect_signal (SIG_CLICKED, change, graph)
    
    frame.add_child (graph, button)
    
    return frame

if __name__ == "__main__":
    # Initialize the drawing window.
    re = Renderer ()
    re.create_screen (450, 500)
    re.title = "Graph2D examples"
    re.color = (234, 228, 223)
    re.add_widget (create_graph2d_view ())
    # Start the main rendering loop.
    re.start ()