Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/graphs.py
blob: 32acaa7f270e07c64a323fe83f8319344e3e6c7e (plain)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import cairo, math
colors = [(1,0,0),(0,1,0),(0,0,1),(.8,.2,0),(0,.8,.3)]



def ln(x):
    try:
        return math.log(x, math.e)
    except:
        return None
def log(x, base = 10):
    try:
        return math.log(x, base)
    except:
        return None
def sin(x):
    if Settings["degrees"]:
        x = math.radians(x) 
    try:
        return math.sin(x)
    except:
        return None
def cos(x):
    if Settings["degrees"]:
        x = math.radians(x)
    try:
        return math.cos(x)
    except:
        return None
def tan(x):
    if Settings["degrees"]:
        x = math.radians(x)
    try:
        return math.tan(x)
    except:
        return None
def csc(x):
    if Settings["degrees"]:
        x = math.radians(x)
    try:
        return 1.0/math.sin(x)
    except:
        return None
def sec(x):
    if Settings["degrees"]:
        x = math.radians(x)
    try:
        return 1.0/math.cos(x)
    except:
        return None
def cot(x):
    if Settings["degrees"]:
        x = math.radians(x)
    try:
        return 1.0/math.tan(x)
    except:
        return None
def sqrt(x):
    try: 
        return math.sqrt(x)
    except:
        return None




def calcXpos(x, settings):
    return float(x - settings["xmin"])/float(settings["xmax"] - settings["xmin"]) * settings["width"]

def calcYpos(y, settings):
    return settings["height"] - float(y - settings["ymin"])/float(settings["ymax"] - settings["ymin"]) * settings["height"]

def calcPosFromPolar(r, theta, settings):
    if settings["degrees"]:
        theta = math.radians(theta)
    x = r*math.cos(theta)
    y = r*math.sin(theta)
    return (calcXpos(x,settings),calcYpos(y,settings))

class FunctionGraph:
    def __init__(self, graph, settings, num):
        self.num = num +1
        self.graph = self.formateGraph(graph)
        self.settings = settings
        self.color = colors[num]
        self.points = self.EvaluateGraph()

    def formateGraph(self, graph):
        formatedGraph = ""
        acceptedNumbers = ["1","2","3","4","5","6","7","8","9","0"]
        acceptedSymbols = [".",",","a","b","c","e","g","i","l","n","o","r","s","t","q","x","*","/","+","-","(",")"]
        for s in graph:
            if s in acceptedNumbers: formatedGraph += s
            if s in acceptedSymbols: formatedGraph += s
            if s == "{" or s == "[": formatedGraph += ("(")
            if s == "}" or s == "}": formatedGraph += (")")
            if s == "^": formatedGraph += ("**")
        i = 0 
        while i < len(formatedGraph)-1:
            if formatedGraph[i] in acceptedNumbers:
                if formatedGraph[i+1] == "x":
                    i += 1
                    formatedGraph = formatedGraph[:i] + "*" + formatedGraph[i:]
            i += 1
        return formatedGraph

    def EvaluateGraph(self):
        graphList = []
        inc = float(self.settings["xmax"]-self.settings["xmin"])/float(self.settings["points"])
        x = self.settings["xmin"]
        while x <= self.settings["xmax"] + inc:
            y = eval(self.graph)
            if y != None:
                graphList.append((calcXpos(x,self.settings),calcYpos(y,self.settings)))
            x += inc
        return graphList

    def draw(self,context):
        context.set_source_rgb(*self.color)
        i = 1
        context.move_to(*self.points[0])
        while i < len(self.points):
            context.line_to(*self.points[i])
            i+=1
        context.stroke()

class PolarGraph:
    def __init__(self, graph, settings, num):
        self.num = num +1
        self.graph = self.formateGraph(graph)
        self.settings = settings
        self.color = colors[num]
        self.points = self.EvaluateGraph()

    def formateGraph(self, graph):
        formatedGraph = ""
        acceptedNumbers = ["1","2","3","4","5","6","7","8","9","0"]
        acceptedSymbols = [".",",","a","b","c","e","g","i","l","n","o","r","s","t","q","x","*","/","+","-","(",")"]
        for s in graph:
            if s in acceptedNumbers: formatedGraph += s
            if s in acceptedSymbols: formatedGraph += s
            if s == "{" or s == "[": formatedGraph += ("(")
            if s == "}" or s == "}": formatedGraph += (")")
            if s == "^": formatedGraph += ("**")
        i = 0 
        while i < len(formatedGraph)-1:
            if formatedGraph[i] in acceptedNumbers:
                if formatedGraph[i+1] == "x":
                    i += 1
                    formatedGraph = formatedGraph[:i] + "*" + formatedGraph[i:]
            i += 1
        return formatedGraph

    def EvaluateGraph(self):
        graphList = []
        inc = float(self.settings["thetamax"]-self.settings["thetamin"])/float(self.settings["points"])
        x = self.settings["thetamin"]
        while x <= self.settings["thetamax"] + inc:
            y = eval(self.graph)
            if y != None:
                graphList.append(calcPosFromPolar(y,x,self.settings))
            x += inc
        return graphList

    def draw(self,context):
        context.set_source_rgb(*self.color)
        i = 1
        context.move_to(*self.points[0])
        while i < len(self.points):
            context.line_to(*self.points[i])
            i+=1
        context.stroke()

class ParametricGraph:
    def __init__(self, graphX, graphY, settings, num):
        self.num = num +1
        self.graphX = self.formateGraph(graphX)
        self.graphY = self.formateGraph(graphY)
        self.settings = settings
        self.color = colors[num]
        self.points = self.EvaluateGraphs()

    def formateGraph(self, graph):
        formatedGraph = ""
        acceptedNumbers = ["1","2","3","4","5","6","7","8","9","0"]
        acceptedSymbols = [".","a","b","c","g","i","l","n","o","r","s","t","q","*","/","+","-","(",")"]
        for s in graph:
            if s in acceptedNumbers: formatedGraph += s
            if s in acceptedSymbols: formatedGraph += s
            if s == "{" or s == "[": formatedGraph += ("(")
            if s == "}" or s == "}": formatedGraph += (")")
            if s == "^": formatedGraph += ("**")
        i = 0 
        while i < len(formatedGraph)-1:
            if formatedGraph[i] in acceptedNumbers:
                if formatedGraph[i+1] == "x":
                    i += 1
                    formatedGraph = formatedGraph[:i] + "*" + formatedGraph[i:]
            i += 1
        return formatedGraph

    def EvaluateGraphs(self):
        graphList = []
        inc = float(self.settings["tmax"]-self.settings["tmin"])/float(self.settings["points"])
        t = self.settings["tmin"]
        while t <= self.settings["tmax"] + inc:
            y = eval(self.graphY)
            x = eval(self.graphX)
            graphList.append((calcXpos(x,self.settings),calcYpos(y,self.settings)))
            t += inc
        return graphList

    def draw(self,context):
        context.set_source_rgb(*self.color)
        i = 1
        context.move_to(*self.points[0])
        while i < len(self.points):
            context.line_to(*self.points[i])
            i+=1
        context.stroke()

class Axis:
    def __init__(self, settings):
        global Settings
        Settings = settings
        width = settings["width"]
        height = settings["height"]
        self.settings = settings
        self.center = (width - width*(float(settings["xmax"])/(float(settings["xmax"]-settings["xmin"]))),
                  height*(float(settings["ymax"])/(float(settings["ymax"]-settings["ymin"]))))
        self.x = self.center[0]
        if self.center[0] < 0:
            self.x = 0
        if self.center[0] > width:
            self.x = width
        self.y = self.center[1]
        if self.center[1] < 0:
            self.y = 0
        if self.center[1] > height:
            self.y = height

    def draw(self,context):
        context.move_to(self.center[0],0)
        context.line_to(self.center[0],self.settings["height"])
        context.stroke()
        context.move_to(0,self.center[1])
        context.line_to(self.settings["width"],self.center[1])
        context.stroke()
        for x in range(int(self.settings["xmin"]), int(self.settings["xmax"])):
            context.move_to(calcXpos(x, self.settings), self.y-5)
            context.line_to(calcXpos(x, self.settings), self.y+5)
            context.stroke()
        for y in range(int(self.settings["ymin"]), int(self.settings["ymax"])):
            context.move_to(self.x-5,calcYpos(y, self.settings))
            context.line_to(self.x+5,calcYpos(y, self.settings))
            context.stroke()