ALGOL 68/prelude/graph 2d.a68: Difference between revisions

m
<lang> -> <syntaxhighlight>
(A simple tookit for producing basic 2d graphs available for various RC tasks)
 
m (<lang> -> <syntaxhighlight>)
 
(2 intermediate revisions by one other user not shown)
Line 1:
<langsyntaxhighlight lang="algol68"># -*- coding: utf-8 -*- #
 
#############################################
Line 17:
INT axis 2d = 2, axis 3d = 3, flex axis = 0;
 
MODE NOARG=BITS; # required when partial parameterisation is not supported #
 
MODE POINT = [axis 2d]GREAL;
MODE POINTDETAIL = STRUCT(STRINGOPT label, POINTOPTION option);
MODE YIELDPOINTPOINTYIELD = PROC(POINT)VOID;
MODE GENPOINTPOINTGEN = PROC(YIELDPOINTPOINTYIELD)VOID;
 
MODE TICK = STRUCT(INT number, GREAL len);
Line 90:
);
 
# A crude CLASS declaration #
MODE GRAPHDD = STRUCT (
PROC(NOARG)ATTRGRAPHDDGRAPHDDMETHODOF attrmethod,
WINDOW window,
GREAL border,
Line 98 ⟶ 99:
[axis 2d]AXIS axis
# FLEX[0]AXISOPT y2, #
# GENPOINTPOINTGEN gen point #
);
 
MODE FUNCTIONINTERVAL = STRUCT(PROC(REAL)REAL function, INTERVAL interval);
MODE CURVE = UNION(GENPOINTPOINTGEN, []POINT, FUNCTIONINTERVAL);
 
MODE ATTRGRAPHDDGRAPHDDMETHODOF = STRUCT(
PROC (NOARG)REF GRAPHDD init,
PROC (LINEOPTION #options#)VOID begin options,
Line 112 ⟶ 113:
PROC (NOARG)VOID decorate,
PROC (NOARG)VOID begin curve,
PROC (GENPOINTPOINTGEN #curve#, LINEOPTION #option#)VOID add curve gen point,
PROC ([]POINT #curve#, LINEOPTION #option#)VOID add curve array point,
PROC (FUNCTIONINTERVAL #function interval#, LINEOPTION #option#)VOID add curve function interval,
Line 119 ⟶ 120:
);
 
OP ATTRMETHODOF = (GRAPHDD graph)ATTRGRAPHDDGRAPHDDMETHODOF: (attrmethod OF graph)(~);
 
GREAL zoom = 1;
Line 127 ⟶ 128:
 
# Bind the Attributes to the Instance #
PROC attrmethod of graph 2d = (REF GRAPHDD self, NOARG skip)ATTRGRAPHDDGRAPHDDMETHODOF:(
init of graph 2d(self,#~#),
begin options of graph 2d(self,),
end options of graph 2d(self,#~#),
init pan and zoom of graph 2d(self,#~#),
pan and zoom of graph 2d(self,),
decorate of graph 2d(self,#~#),
begin curve of graph 2d(self,#~#),
add curve gen point of graph 2d(self,,),
add curve array point of graph 2d(self,,),
add curve function interval of graph 2d(self,,),
add curve of graph 2d(self,,),
end curve of graph 2d(self,#~#)
);
 
PROC init of graph 2d = (REF GRAPHDD self, NOARG skip)REF GRAPHDD: (
attrmethod OF self := attrmethod of graph 2d(self,#~#); # Hmmm... #
 
title OF self := sub title OF self := EMPTY;
Line 216 ⟶ 217:
 
# label x axis axis #
point := (pan and zoom OF (attr OFMETHODOF self)(~))((x axis LWB self, x axis offset));
draw move (window, label start, point[y axis]-pan);
draw text (window, "c", "c", (label OF axis OF self)[x axis] ORELSE "X Axis");
Line 224 ⟶ 225:
 
# draw x axis axis #
point := (pan and zoom OF (attr OFMETHODOF self)(~))((x axis UPB self, x axis offset));
draw line point (window, point);
draw move (window, point[x axis], point[y axis]-pan);
Line 230 ⟶ 231:
 
# label y axis axis #
point := (pan and zoom OF (attr OFMETHODOF self)(~))((y axis offset, y axis LWB self));
draw text angle (window, 90);
draw move (window, point[x axis]-pan, label start);
Line 239 ⟶ 240:
# draw y axis axis #
draw move point(window, point);
point := (pan and zoom OF (attr OFMETHODOF self)(~))((y axis offset, y axis UPB self));
draw line point(window, point);
draw move (window, point[x axis]-pan, point[y axis]);
Line 255 ⟶ 256:
 
draw device (window, type OF window OF self, sprintf(($g(0)"x axis"g(0)$, pixels OF window OF self)));
(init pan and zoom OF (attr OFMETHODOF self)(~))(~);
(decorate OF (attr OFMETHODOF self)(~))(~)
);
 
PROC add curve gen point of graph 2d = (REF GRAPHDD self, GENPOINTPOINTGEN gen point, LINEOPTION option)VOID:(
REF FILE window = file OF window OF self;
BOOL first := TRUE;
POINT prev specimen;
(begin options OF (attr OFMETHODOF self)(~))(option);
#FOR POINT point IN # gen point #DO#(
## (POINT point)VOID: (
POINT next specimen = (pan and zoom OF (attr OFMETHODOF self)(~))(point);
IF UPB next specimen = 2 AND FALSE THEN # simple case: the single line #
IF first THEN first := FALSE; draw move point ELSE draw line point FI (window, next specimen)
Line 281 ⟶ 282:
FI
# OD#));
(end options OF (attr OFMETHODOF self)(~))(~)
);
 
PROC add curve array point of graph 2d = (REF GRAPHDD self, []POINT curve, LINEOPTION option)VOID:(
PROC gen curve = (YIELDPOINTPOINTYIELD yield)VOID:
FOR i TO UPB curve DO yield(curve[i]) OD;
(add curve gen point OF (attr OFMETHODOF self)(~))(gen curve, option)
);
 
PROC add curve function interval of graph 2d = (REF GRAPHDD self, FUNCTIONINTERVAL function interval, LINEOPTION option)VOID:(
PROC gen curve = (YIELDPOINTPOINTYIELD yield)VOID:(
INT n steps = 100;
REAL x := lwb OF interval OF function interval;
Line 300 ⟶ 301:
OD
);
(add curve gen point OF (attr OFMETHODOF self)(~))(gen curve, option)
),
 
PROC add curve of graph 2d = (REF GRAPHDD self, CURVE curve, LINEOPTION option)VOID:(
CASE curve IN
(GENPOINTPOINTGEN gen): (add curve gen point OF (attr OFMETHODOF self)(~))(gen, option),
([]POINT array): (add curve array point OF (attr OFMETHODOF self)(~))(array, option),
(FUNCTIONINTERVAL function interval): (add curve function interval OF (attr OFMETHODOF self)(~))(function interval, option)
OUT
raise undefined("add curve type")
Line 321 ⟶ 322:
OP INIT = (REF GRAPHDD object)REF GRAPHDD: init of graph 2d(object,~);
 
SKIP</syntaxhighlight>
SKIP
3,021

edits