-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrapher.ml
67 lines (59 loc) · 1.68 KB
/
grapher.ml
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
open Graphics
let string_of_float f =
let st = string_of_float f in
if st.[(String.length st) - 1] = '.'
then st ^ "0"
else st
let set_up title minx maxx miny maxy assoc_lst =
Graphics.open_graph "";
resize_window 800 800;
set_window_title ("OCalc - graphing " ^ title);
set_color black;
set_line_width 1;
moveto 80 720;
set_text_size 1000;
draw_string title;
(*plot axes*)
let xoffsets = (maxx -. minx) /. 10. in
let yoffsets = (maxy -. miny) /. 10. in
moveto 400 0;
lineto 400 800;
moveto 0 400;
lineto 800 400;
(*set markers*)
let x = ref 0 in
let xm = ref 0. in
while !x < 800 do
moveto !x 385;
lineto !x 415;
moveto !x 360;
draw_string ((minx +. !xm) |> string_of_float);
xm := (!xm +. xoffsets);
x := (!x + 80);
done;
let y = ref 0 in
let ym = ref 0. in
while !y < 800 do
moveto 385 !y;
lineto 415 !y;
moveto 415 !y;
draw_string ((miny +. !ym) |> string_of_float);
ym := (!ym +. yoffsets);
y := (!y + 80);
done;
let transformx x = ((x -. minx) /. (maxx -. minx)) *. 800. in
let transformy y = ((y -. miny) /. (maxy -. miny)) *. 800. in
let finallst = List.map (fun (x, y) -> (transformx x),(transformy y)) assoc_lst in
let filterlst = List.filter (fun (x, y) -> y >0. && y < 800.) finallst in
let firstx = List.hd filterlst |> fst |> int_of_float in
let firsty = List.hd filterlst |> snd |> int_of_float in
moveto firstx firsty;
set_color magenta;
set_line_width 3;
for i=0 to (List.length filterlst)-1 do
let y = (i |> List.nth filterlst |> snd |> int_of_float) in
if y > 0 && y < 800 then
lineto (i |> List.nth filterlst |> fst |> int_of_float) y
else ()
done;
()