tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
7
Task/Plot-coordinate-pairs/0DESCRIPTION
Normal file
7
Task/Plot-coordinate-pairs/0DESCRIPTION
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
Plot a function represented as `x', `y' numerical arrays.
|
||||
|
||||
Post link to your resulting image for input arrays (see [[Query Performance|'''Example''' section for Python language on ''Query Performance'' page]]):
|
||||
x = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
|
||||
y = {2.7, 2.8, 31.4, 38.1, 58.0, 76.2, 100.5, 130.0, 149.3, 180.0};
|
||||
|
||||
This task is intended as a subtask for [[Measure relative performance of sorting algorithms implementations]].
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
#!/usr/bin/algol68g-full --script #
|
||||
# -*- coding: utf-8 -*- #
|
||||
|
||||
PR READ "prelude/errata.a68" PR;
|
||||
PR READ "prelude/exception.a68" PR;
|
||||
PR READ "prelude/math_lib.a68" PR;
|
||||
|
||||
CO REQUIRED BY "prelude/graph_2d.a68" CO
|
||||
MODE GREAL= REAL; # single precision #
|
||||
FORMAT greal repr = $g(-3,0)$;
|
||||
PR READ "prelude/graph_2d.a68" PR;
|
||||
|
||||
[]REAL x = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
|
||||
[]REAL y = (2.7, 2.8, 31.4, 38.1, 58.0, 76.2, 100.5, 130.0, 149.3, 180.0);
|
||||
|
||||
test:(
|
||||
REF GRAPHDD test graph = INIT LOC GRAPHDD;
|
||||
type OF window OF test graph := "gif"; # or gif, ps, X, pnm etc #
|
||||
|
||||
title OF test graph := "Plot coordinate pairs";
|
||||
sub title OF test graph := "Algol68";
|
||||
|
||||
interval OF (axis OF test graph)[x axis] := (0, 8);
|
||||
label OF (axis OF test graph)[x axis] := "X axis";
|
||||
|
||||
interval OF (axis OF test graph)[y axis] := (0, 200);
|
||||
label OF (axis OF test graph)[y axis] := "Y axis";
|
||||
|
||||
PROC curve = (POINTYIELD yield)VOID:
|
||||
FOR i TO UPB x DO yield((x[i],y[i])) OD;
|
||||
|
||||
(begin curve OF (METHODOF test graph))(~);
|
||||
(add curve OF (METHODOF test graph))(curve, (red,solid));
|
||||
(end curve OF (METHODOF test graph))(~)
|
||||
);
|
||||
|
||||
PR READ "postlude/exception.a68" PR
|
||||
46
Task/Plot-coordinate-pairs/Ada/plot-coordinate-pairs.ada
Normal file
46
Task/Plot-coordinate-pairs/Ada/plot-coordinate-pairs.ada
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
with Gtk.Main;
|
||||
with Gtk.Window; use Gtk.Window;
|
||||
with Gtk.Widget; use Gtk.Widget;
|
||||
with Gtk.Handlers; use Gtk.Handlers;
|
||||
with Glib; use Glib;
|
||||
with Gtk.Extra.Plot; use Gtk.Extra.Plot;
|
||||
with Gtk.Extra.Plot_Data; use Gtk.Extra.Plot_Data;
|
||||
with Gtk.Extra.Plot_Canvas; use Gtk.Extra.Plot_Canvas;
|
||||
with Gtk.Extra.Plot_Canvas.Plot; use Gtk.Extra.Plot_Canvas.Plot;
|
||||
|
||||
procedure PlotCoords is
|
||||
package Handler is new Callback (Gtk_Widget_Record);
|
||||
|
||||
Window : Gtk_Window;
|
||||
Plot : Gtk_Plot;
|
||||
PCP : Gtk_Plot_Canvas_Plot;
|
||||
Canvas : Gtk_Plot_Canvas;
|
||||
PlotData : Gtk_Plot_Data;
|
||||
x, y, dx, dy : Gdouble_Array_Access;
|
||||
|
||||
procedure ExitMain (Object : access Gtk_Widget_Record'Class) is
|
||||
begin
|
||||
Destroy (Object); Gtk.Main.Main_Quit;
|
||||
end ExitMain;
|
||||
|
||||
begin
|
||||
x := new Gdouble_Array'(0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0);
|
||||
y := new Gdouble_Array'(2.7, 2.8, 31.4, 38.1, 58.0, 76.2, 100.5, 130.0, 149.3, 180.0);
|
||||
Gtk.Main.Init;
|
||||
Gtk_New (Window);
|
||||
Set_Title (Window, "Plot coordinate pairs with GtkAda");
|
||||
Gtk_New (PlotData);
|
||||
Set_Points (PlotData, x, y, dx, dy);
|
||||
Gtk_New (Plot);
|
||||
Add_Data (Plot, PlotData);
|
||||
Autoscale (Plot); Show (PlotData);
|
||||
Hide_Legends (Plot);
|
||||
Gtk_New (PCP, Plot); Show (Plot);
|
||||
Gtk_New (Canvas, 500, 500); Show (Canvas);
|
||||
Put_Child (Canvas, PCP, 0.15, 0.15, 0.85, 0.85);
|
||||
Add (Window, Canvas);
|
||||
Show_All (Window);
|
||||
Handler.Connect (Window, "destroy",
|
||||
Handler.To_Marshaller (ExitMain'Access));
|
||||
Gtk.Main.Main;
|
||||
end PlotCoords;
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
DIM x(9), y(9)
|
||||
x() = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
|
||||
y() = 2.7, 2.8, 31.4, 38.1, 58.0, 76.2, 100.5, 130.0, 149.3, 180.0
|
||||
|
||||
ORIGIN 100,100
|
||||
VDU 23,23,2;0;0;0;
|
||||
VDU 5
|
||||
|
||||
FOR x = 1 TO 9
|
||||
GCOL 7 : LINE 100*x,720,100*x,0
|
||||
GCOL 0 : PLOT 0,-10,-4 : PRINT ; x ;
|
||||
NEXT
|
||||
|
||||
FOR y = 20 TO 180 STEP 20
|
||||
GCOL 7 : LINE 900,4*y,0,4*y
|
||||
GCOL 0 : PLOT 0,-212,20 : PRINT y ;
|
||||
NEXT
|
||||
|
||||
LINE 0,0,0,720
|
||||
LINE 0,0,900,0
|
||||
|
||||
GCOL 4
|
||||
FOR i% = 0 TO 9
|
||||
IF i%=0 THEN
|
||||
MOVE 100*x(i%),4*y(i%)
|
||||
ELSE
|
||||
DRAW 100*x(i%),4*y(i%)
|
||||
ENDIF
|
||||
NEXT
|
||||
117
Task/Plot-coordinate-pairs/C/plot-coordinate-pairs-1.c
Normal file
117
Task/Plot-coordinate-pairs/C/plot-coordinate-pairs-1.c
Normal file
|
|
@ -0,0 +1,117 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include <plot.h>
|
||||
|
||||
#define NP 10
|
||||
double x[NP] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
|
||||
double y[NP] = {2.7, 2.8, 31.4, 38.1, 58.0, 76.2, 100.5, 130.0, 149.3, 180.0};
|
||||
|
||||
void minmax(double *x, double *y,
|
||||
double *minx, double *maxx,
|
||||
double *miny, double *maxy, int n)
|
||||
{
|
||||
int i;
|
||||
|
||||
*minx = *maxx = x[0];
|
||||
*miny = *maxy = y[0];
|
||||
for(i=1; i < n; i++) {
|
||||
if ( x[i] < *minx ) *minx = x[i];
|
||||
if ( x[i] > *maxx ) *maxx = x[i];
|
||||
if ( y[i] < *miny ) *miny = y[i];
|
||||
if ( y[i] > *maxy ) *maxy = y[i];
|
||||
}
|
||||
}
|
||||
|
||||
/* likely we must play with this parameter to make the plot looks better
|
||||
when using different set of data */
|
||||
#define YLAB_HEIGHT_F 0.1
|
||||
#define XLAB_WIDTH_F 0.2
|
||||
#define XDIV (NP*1.0)
|
||||
#define YDIV (NP*1.0)
|
||||
#define EXTRA_W 0.01
|
||||
#define EXTRA_H 0.01
|
||||
#define DOTSCALE (1.0/150.0)
|
||||
|
||||
#define MAXLABLEN 32
|
||||
|
||||
#define PUSHSCALE(X,Y) pl_fscale((X),(Y))
|
||||
#define POPSCALE(X,Y) pl_fscale(1.0/(X), 1.0/(Y))
|
||||
#define FMOVESCALE(X,Y) pl_fmove((X)/sx, (Y)/sy)
|
||||
|
||||
int main()
|
||||
{
|
||||
int plotter, i;
|
||||
double minx, miny, maxx, maxy;
|
||||
double lx, ly;
|
||||
double xticstep, yticstep, nx, ny;
|
||||
double sx, sy;
|
||||
|
||||
char labs[MAXLABLEN+1];
|
||||
|
||||
plotter = pl_newpl("png", NULL, stdout, NULL);
|
||||
if ( plotter < 0 ) exit(1);
|
||||
pl_selectpl(plotter);
|
||||
if ( pl_openpl() < 0 ) exit(1);
|
||||
|
||||
/* determines minx, miny, maxx, maxy */
|
||||
minmax(x, y, &minx, &maxx, &miny, &maxy, NP);
|
||||
|
||||
lx = maxx - minx;
|
||||
ly = maxy - miny;
|
||||
pl_fspace(floor(minx) - XLAB_WIDTH_F * lx, floor(miny) - YLAB_HEIGHT_F * ly,
|
||||
ceil(maxx) + EXTRA_W * lx, ceil(maxy) + EXTRA_H * ly);
|
||||
|
||||
/* compute x,y-ticstep */
|
||||
xticstep = (ceil(maxx) - floor(minx)) / XDIV;
|
||||
yticstep = (ceil(maxy) - floor(miny)) / YDIV;
|
||||
|
||||
pl_flinewidth(0.25);
|
||||
|
||||
/* compute scale factors to adjust aspect */
|
||||
if ( lx < ly ) {
|
||||
sx = lx/ly;
|
||||
sy = 1.0;
|
||||
} else {
|
||||
sx = 1.0;
|
||||
sy = ly/lx;
|
||||
}
|
||||
|
||||
pl_erase();
|
||||
|
||||
/* a frame... */
|
||||
pl_fbox(floor(minx), floor(miny),
|
||||
ceil(maxx), ceil(maxy));
|
||||
|
||||
/* labels and "tics" */
|
||||
pl_fontname("HersheySerif");
|
||||
for(ny=floor(miny); ny < ceil(maxy); ny += yticstep) {
|
||||
pl_fline(floor(minx), ny, ceil(maxx), ny);
|
||||
snprintf(labs, MAXLABLEN, "%6.2lf", ny);
|
||||
FMOVESCALE(floor(minx) - XLAB_WIDTH_F * lx, ny);
|
||||
PUSHSCALE(sx,sy);
|
||||
pl_label(labs);
|
||||
POPSCALE(sx,sy);
|
||||
}
|
||||
for(nx=floor(minx); nx < ceil(maxx); nx += xticstep) {
|
||||
pl_fline(nx, floor(miny), nx, ceil(maxy));
|
||||
snprintf(labs, MAXLABLEN, "%6.2lf", nx);
|
||||
FMOVESCALE(nx, floor(miny));
|
||||
PUSHSCALE(sx,sy);
|
||||
pl_ftextangle(-90);
|
||||
pl_alabel('l', 'b', labs);
|
||||
POPSCALE(sx,sy);
|
||||
}
|
||||
|
||||
/* plot data "point" */
|
||||
pl_fillcolorname("red");
|
||||
pl_filltype(1);
|
||||
for(i=0; i < NP; i++)
|
||||
{
|
||||
pl_fbox(x[i] - lx * DOTSCALE, y[i] - ly * DOTSCALE,
|
||||
x[i] + lx * DOTSCALE, y[i] + ly * DOTSCALE);
|
||||
}
|
||||
|
||||
pl_flushpl();
|
||||
pl_closepl();
|
||||
}
|
||||
131
Task/Plot-coordinate-pairs/C/plot-coordinate-pairs-2.c
Normal file
131
Task/Plot-coordinate-pairs/C/plot-coordinate-pairs-2.c
Normal file
|
|
@ -0,0 +1,131 @@
|
|||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
#include <string.h>
|
||||
|
||||
#define N 40
|
||||
double x[N], y[N];
|
||||
|
||||
void minmax(double x[], int len, double *base, double *step, int *nstep)
|
||||
{
|
||||
int i;
|
||||
double diff, minv, maxv;
|
||||
*step = 1;
|
||||
|
||||
minv = maxv = x[0];
|
||||
for (i = 1; i < len; i++) {
|
||||
if (minv > x[i]) minv = x[i];
|
||||
if (maxv < x[i]) maxv = x[i];
|
||||
}
|
||||
if (minv == maxv) {
|
||||
minv = floor(minv);
|
||||
maxv = ceil(maxv);
|
||||
if (minv == maxv) {
|
||||
minv--;
|
||||
maxv++;
|
||||
}
|
||||
} else {
|
||||
diff = maxv - minv;
|
||||
while (*step < diff) *step *= 10;
|
||||
while (*step > diff) *step /= 10;
|
||||
if (*step > diff / 2) *step /= 5;
|
||||
else if (*step > diff / 5) *step /= 2;
|
||||
}
|
||||
|
||||
*base = floor(minv / *step) * *step;
|
||||
*nstep = ceil(maxv / *step) - floor(minv / *step);
|
||||
}
|
||||
|
||||
/* writes an eps with 400 x 300 dimention, using 12 pt font */
|
||||
#define CHARH 12
|
||||
#define CHARW 6
|
||||
#define DIMX 398
|
||||
#define DIMY (300 - CHARH)
|
||||
#define BOTY 20.
|
||||
int plot(double x[], double y[], int len, char *spec)
|
||||
{
|
||||
int nx, ny, i;
|
||||
double sx, sy, x0, y0;
|
||||
char buf[100];
|
||||
int dx, dy, lx, ly;
|
||||
double ofs_x, ofs_y, grid_x;
|
||||
|
||||
minmax(x, len, &x0, &sx, &nx);
|
||||
minmax(y, len, &y0, &sy, &ny);
|
||||
|
||||
dx = -log10(sx);
|
||||
dy = -log10(sy);
|
||||
|
||||
ly = 0;
|
||||
for (i = 0; i <= ny; i++) {
|
||||
sprintf(buf, "%g\n", y0 + i * sy);
|
||||
if (strlen(buf) > ly) ly = strlen(buf);
|
||||
}
|
||||
ofs_x = ly * CHARW;
|
||||
|
||||
printf("%%!PS-Adobe-3.0\n%%%%BoundingBox: 0 0 400 300\n"
|
||||
"/TimesRoman findfont %d scalefont setfont\n"
|
||||
"/rl{rlineto}def /l{lineto}def /s{setrgbcolor}def "
|
||||
"/rm{rmoveto}def /m{moveto}def /st{stroke}def\n",
|
||||
CHARH);
|
||||
for (i = 0; i <= ny; i++) {
|
||||
ofs_y = BOTY + (DIMY - BOTY) / ny * i;
|
||||
printf("0 %g m (%*.*f) show\n",
|
||||
ofs_y - 4, ly, dy, y0 + i * sy);
|
||||
if (i) printf("%g %g m 7 0 rl st\n",
|
||||
ofs_x, ofs_y);
|
||||
}
|
||||
printf("%g %g m %g %g l st\n", ofs_x, BOTY, ofs_x, ofs_y);
|
||||
|
||||
for (i = 0; i <= nx; i++) {
|
||||
sprintf(buf, "%g", x0 + i * sx);
|
||||
lx = strlen(buf);
|
||||
grid_x = ofs_x + (DIMX - ofs_x) / nx * i;
|
||||
|
||||
printf("%g %g m (%s) show\n", grid_x - CHARW * lx / 2,
|
||||
BOTY - 12, buf);
|
||||
if (i) printf("%g %g m 0 7 rl st\n", grid_x, BOTY);
|
||||
}
|
||||
printf("%g %g m %g %g l st\n", ofs_x, BOTY, grid_x, BOTY);
|
||||
|
||||
if (strchr(spec, 'r')) printf("1 0 0 s\n");
|
||||
else if (strchr(spec, 'b')) printf("0 0 1 s\n");
|
||||
else if (strchr(spec, 'g')) printf("0 1 0 s\n");
|
||||
else if (strchr(spec, 'm')) printf("1 0 1 s\n");
|
||||
|
||||
if (strchr(spec, 'o'))
|
||||
printf("/o { m 0 3 rm 3 -3 rl -3 -3 rl -3 3 rl closepath st} def "
|
||||
".5 setlinewidth\n");
|
||||
|
||||
if (strchr(spec, '-')) {
|
||||
for (i = 0; i < len; i++) {
|
||||
printf("%g %g %s ",
|
||||
(x[i] - x0) / (sx * nx) * (DIMX - ofs_x) + ofs_x,
|
||||
(y[i] - y0) / (sy * ny) * (DIMY - BOTY) + BOTY,
|
||||
i ? "l" : "m");
|
||||
}
|
||||
printf("st\n");
|
||||
}
|
||||
|
||||
if (strchr(spec, 'o'))
|
||||
for (i = 0; i < len; i++) {
|
||||
printf("%g %g o ",
|
||||
(x[i] - x0) / (sx * nx) * (DIMX - ofs_x) + ofs_x,
|
||||
(y[i] - y0) / (sy * ny) * (DIMY - BOTY) + BOTY);
|
||||
}
|
||||
|
||||
printf("showpage\n%%EOF");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < N; i++) {
|
||||
x[i] = (double)i / N * 3.14159 * 6;
|
||||
y[i] = -1337 + (exp(x[i] / 10) + cos(x[i])) / 100;
|
||||
}
|
||||
/* string parts: any of "rgbm": color; "-": draw line; "o": draw symbol */
|
||||
plot(x, y, N, "r-o");
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
import groovy.swing.SwingBuilder
|
||||
import javax.swing.JFrame
|
||||
import org.jfree.chart.ChartFactory
|
||||
import org.jfree.chart.ChartPanel
|
||||
import org.jfree.data.xy.XYSeries
|
||||
import org.jfree.data.xy.XYSeriesCollection
|
||||
import org.jfree.chart.plot.PlotOrientation
|
||||
|
||||
def chart = {
|
||||
x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
||||
y = [2.7, 2.8, 31.4, 38.1, 58.0, 76.2, 100.5, 130.0, 149.3, 180.0]
|
||||
|
||||
def series = new XYSeries('plots')
|
||||
[x, y].transpose().each { x, y -> series.add x, y }
|
||||
|
||||
def labels = ["Plot Demo", "X", "Y"]
|
||||
def data = new XYSeriesCollection(series)
|
||||
def options = [false, true, false]
|
||||
|
||||
def chart = ChartFactory.createXYLineChart(*labels, data, PlotOrientation.VERTICAL, *options)
|
||||
new ChartPanel(chart)
|
||||
}
|
||||
|
||||
new SwingBuilder().edt {
|
||||
frame(title:'Plot coordinate pairs', defaultCloseOperation:JFrame.EXIT_ON_CLOSE, pack:true, show:true) {
|
||||
widget(chart())
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
import Graphics.Gnuplot.Simple
|
||||
|
||||
pnts = [2.7, 2.8, 31.4, 38.1, 58.0, 76.2, 100.5, 130.0, 149.3, 180.0]
|
||||
|
||||
doPlot = plotPathStyle [ ( Title "plotting dots" )]
|
||||
(PlotStyle Points (CustomStyle [])) (zip [0..] pnts)
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
REAL :: n=10, x(n), y(n)
|
||||
|
||||
x = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
|
||||
y = (2.7, 2.8, 31.4, 38.1, 58.0, 76.2, 100.5, 130.0, 149.3, 180.0)
|
||||
|
||||
WINDOW(WINdowhandle=wh, Width=-300, Height=-300, X=1, TItle='Rosetta')
|
||||
AXIS(WINdowhandle=wh, Title='x values', Yaxis, Title='y values')
|
||||
LINE(X=x, Y=y, SymbolDiameter=2)
|
||||
86
Task/Plot-coordinate-pairs/Icon/plot-coordinate-pairs.icon
Normal file
86
Task/Plot-coordinate-pairs/Icon/plot-coordinate-pairs.icon
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
link printf,numbers
|
||||
|
||||
procedure main()
|
||||
x := [0., 1., 2., 3., 4., 5., 6., 7., 8., 9.]
|
||||
y := [2.7, 2.8, 31.4, 38.1, 58.0, 76.2, 100.5, 130.0, 149.3, 180.0]
|
||||
Plot(x,y,600,400)
|
||||
end
|
||||
|
||||
$define POINTR 2 # Point Radius
|
||||
$define POINTC "red" # Point Colour
|
||||
$define GRIDC "grey" # grid colour
|
||||
$define AXISC "black" # axis/label colour
|
||||
$define BORDER 60 # per side border
|
||||
$define TICKS 5. # grid ticks per axis
|
||||
$define AXISFH 20 # font height for axis labels
|
||||
|
||||
procedure Plot(x,y,cw,ch)
|
||||
|
||||
/cw := 700 # default dimensions
|
||||
/ch := 400
|
||||
uw := cw-BORDER*2 # usable dimensions
|
||||
uh := ch-BORDER*2
|
||||
|
||||
wparms := ["Plot","g",
|
||||
sprintf("size=%d,%d",cw,ch),
|
||||
"bg=white"] # base window parms
|
||||
|
||||
dx := sprintf("dx=%d",BORDER) # grid origin
|
||||
dy := sprintf("dy=%d",BORDER)
|
||||
|
||||
&window := open!wparms | stop("Unable to open window")
|
||||
X := scale(x,uw) # scale data to usable space
|
||||
Y := scale(y,uh,"invert")
|
||||
|
||||
WAttrib(dx,dy) # set origin=grid & draw grid
|
||||
every x := (X.tickfrom to X.tickto by X.tick) * X.tickscale do {
|
||||
if x = 0 then Fg(AXISC) else Fg(GRIDC)
|
||||
DrawLine(x,Y.tickfrom*Y.tickscale,x,Y.tickto*Y.tickscale)
|
||||
}
|
||||
every y := (Y.tickfrom to Y.tickto by Y.tick) * Y.tickscale do {
|
||||
if y = uh then Fg(AXISC) else Fg(GRIDC)
|
||||
DrawLine(X.tickfrom*X.tickscale,y,X.tickto*X.tickscale,y)
|
||||
}
|
||||
|
||||
Fg(POINTC) # draw data points ....
|
||||
every i := 1 to *X.scaled do
|
||||
FillCircle(X.scaled[i],Y.scaled[i],POINTR)
|
||||
|
||||
Fg(AXISC) # label grid
|
||||
WAttrib(dx,"dy=0") # label X axis
|
||||
Font(sprintf("Helvetica,%d",AXISFH))
|
||||
ytxt := ch-BORDER+1+(WAttrib("ascent") - WAttrib("descent"))/2
|
||||
|
||||
every x := X.tickscale * (xv := X.tickfrom to X.tickto by X.tick) do
|
||||
DrawString(x - TextWidth(xv)/2, ytxt + integer(AXISFH*1.5),xv)
|
||||
|
||||
WAttrib("dx=0",dy) # label Y axis
|
||||
every y := Y.tickscale * (yv := Y.tickfrom to Y.tickto by Y.tick) do
|
||||
DrawString(BORDER/2 - TextWidth(yv)/2, ytxt - BORDER - y,yv)
|
||||
|
||||
WriteImage(sprintf("PlotPoints-%d.gif",&now)) # save image
|
||||
|
||||
WAttrib("dx=0","dy=0") # close off nicely
|
||||
Font("Helvetica,10")
|
||||
DrawString(10,ch-5,"Right click to exit")
|
||||
until Event() == &rpress # wait for left mouse button
|
||||
close(&window)
|
||||
end
|
||||
|
||||
record scaledata(low,high,range,pix,raw,scaled,tick,tickfrom,tickto,tickscale)
|
||||
|
||||
procedure scale(data,pix,opts[])
|
||||
P :=scaledata( pmin := min!data, pmax := max!data,
|
||||
prange := real(pmax-pmin), pix,
|
||||
data,q :=[])
|
||||
|
||||
/ticks := TICKS
|
||||
P.tick := ceil(prange/(10^(k:=floor(log(prange,10))))*(10^k)/ticks)
|
||||
P.tickfrom := P.tick*floor(pmin/P.tick)
|
||||
P.tickto := P.tick*ceil(pmax/P.tick)
|
||||
P.tickscale := real(pix)/(P.tickto-P.tickfrom)
|
||||
every put(q,integer((!data-P.tickfrom)*P.tickscale))
|
||||
if !opts == "invert" then # invert is for y
|
||||
every q[i := 1 to *q] := pix - q[i]
|
||||
return P
|
||||
end
|
||||
4
Task/Plot-coordinate-pairs/J/plot-coordinate-pairs.j
Normal file
4
Task/Plot-coordinate-pairs/J/plot-coordinate-pairs.j
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
require 'plot'
|
||||
X=: i.10
|
||||
Y=: 2.7 2.8 31.4 38.1 58.0 76.2 100.5 130.0 149.3 180.0
|
||||
'dot; pensize 2.4' plot X;Y
|
||||
89
Task/Plot-coordinate-pairs/Java/plot-coordinate-pairs.java
Normal file
89
Task/Plot-coordinate-pairs/Java/plot-coordinate-pairs.java
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
import java.awt.geom.*;
|
||||
import javax.swing.JApplet;
|
||||
import javax.swing.JFrame;
|
||||
public class Plot2d extends JApplet {
|
||||
double[] xi;
|
||||
double[] yi;
|
||||
public Plot2d(double[] x, double[] y) {
|
||||
this.xi = x;
|
||||
this.yi = y;
|
||||
}
|
||||
public static double max(double[] t) {
|
||||
double maximum = t[0];
|
||||
for (int i = 1; i < t.length; i++) {
|
||||
if (t[i] > maximum) {
|
||||
maximum = t[i];
|
||||
}
|
||||
}
|
||||
return maximum;
|
||||
}
|
||||
public static double min(double[] t) {
|
||||
double minimum = t[0];
|
||||
for (int i = 1; i < t.length; i++) {
|
||||
if (t[i] < minimum) {
|
||||
minimum = t[i];
|
||||
}
|
||||
}
|
||||
return minimum;
|
||||
}
|
||||
public void init() {
|
||||
setBackground(Color.white);
|
||||
setForeground(Color.white);
|
||||
}
|
||||
public void paint(Graphics g) {
|
||||
Graphics2D g2 = (Graphics2D) g;
|
||||
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
|
||||
RenderingHints.VALUE_ANTIALIAS_ON);
|
||||
g2.setPaint(Color.black);
|
||||
int x0 = 70;
|
||||
int y0 = 10;
|
||||
int xm = 670;
|
||||
int ym = 410;
|
||||
int xspan = xm - x0;
|
||||
int yspan = ym - y0;
|
||||
double xmax = max(xi);
|
||||
double xmin = min(xi);
|
||||
double ymax = max(yi);
|
||||
double ymin = min(yi);
|
||||
g2.draw(new Line2D.Double(x0, ym, xm, ym));
|
||||
g2.draw(new Line2D.Double(x0, ym, x0, y0));
|
||||
for (int j = 0; j < 5; j++) {
|
||||
int interv = 4;
|
||||
g2.drawString("" + (j * (xmax - xmin) / interv + xmin), j * xspan / interv + x0 - 10, ym + 20);
|
||||
g2.drawString("" + (j * (ymax - ymin) / interv + ymin), x0 - 20 - (int) (9 * Math.log10(ymax)),
|
||||
ym - j * yspan / interv + y0 - 5);
|
||||
g2.draw(new Line2D.Double(j * xspan / interv + x0, ym, j * xspan / interv + x0, ym + 5));
|
||||
g2.draw(new Line2D.Double(x0 - 5, j * yspan / interv + y0, x0, j * yspan / interv + y0));
|
||||
}
|
||||
for (int i = 0; i < xi.length; i++) {
|
||||
int f = (int) ((xi[i] - xmin) * xspan / (xmax - xmin));
|
||||
int h = (int) (((ymax - ymin) - (yi[i] - ymin)) * yspan / (ymax - ymin));
|
||||
g2.drawString("o", x0 + f - 3, h + 14);
|
||||
}
|
||||
for (int i = 0; i < xi.length - 1; i++) {
|
||||
int f = (int) ((xi[i] - xmin) * xspan / (xmax - xmin));
|
||||
int f2 = (int) ((xi[i + 1] - xmin) * xspan / (xmax - xmin));
|
||||
int h = (int) (((ymax - ymin) - (yi[i] - ymin)) * yspan / (ymax - ymin));
|
||||
int h2 = (int) (((ymax - ymin) - (yi[i + 1] - ymin)) * yspan / (ymax - ymin));
|
||||
g2.draw(new Line2D.Double(f + x0, h + y0, f2 + x0, h2 + y0));
|
||||
}
|
||||
}
|
||||
public static void main(String args[]) {
|
||||
JFrame f = new JFrame("ShapesDemo2D");
|
||||
f.addWindowListener(new WindowAdapter() {
|
||||
public void windowClosing(WindowEvent e) {
|
||||
System.exit(0);
|
||||
}
|
||||
});
|
||||
double[] r = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
|
||||
double[] t = {2.7, 2.8, 31.4, 38.1, 58.0, 76.2, 100.5, 130.0, 149.3, 180.09};
|
||||
JApplet applet = new Plot2d(r, t);
|
||||
f.getContentPane().add("Center", applet);
|
||||
applet.init();
|
||||
f.pack();
|
||||
f.setSize(new Dimension(720, 480));
|
||||
f.show();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
'Plotting coordinate pairs MainWin - Style
|
||||
For i = 0 To 9
|
||||
x(i) = i
|
||||
Next i
|
||||
|
||||
y(0) = 2.7
|
||||
y(1) = 2.8
|
||||
y(2) = 31.4
|
||||
y(3) = 38.1
|
||||
y(4) = 58.0
|
||||
y(5) = 76.2
|
||||
y(6) = 100.5
|
||||
y(7) = 130.0
|
||||
y(8) = 149.3
|
||||
y(9) = 180.0
|
||||
|
||||
Locate 4, 22
|
||||
For i = 0 To 9
|
||||
Locate ((i * 4) + 2), 22
|
||||
Print i
|
||||
Next i
|
||||
|
||||
For i = 0 To 20 Step 2
|
||||
Locate 0, (21 - i)
|
||||
Print (i * 10)
|
||||
Next i
|
||||
|
||||
|
||||
For i = 0 To 9
|
||||
Locate (x(i) * 4) + 2, (21 - (y(i)/ 10))
|
||||
Print "."
|
||||
Next i
|
||||
|
||||
End
|
||||
|
|
@ -0,0 +1,63 @@
|
|||
nomainwin
|
||||
|
||||
DATA 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
|
||||
DATA 2.7, 2.8, 31.4, 38.1, 58.0, 76.2, 100.5, 130.0, 149.3, 180.0
|
||||
|
||||
For i = 0 To 9
|
||||
READ tmp: x( i) = tmp
|
||||
Next i
|
||||
|
||||
For i = 0 To 9
|
||||
READ tmp: y( i) = tmp
|
||||
Next i
|
||||
|
||||
'Plotting coordinate pairs
|
||||
WindowHeight = 500
|
||||
WindowWidth = 430
|
||||
Open "Plot coordinate pairs" For Graphics_nsb_nf As #gwin
|
||||
#gwin "trapclose [quit.gwin]"
|
||||
#gwin "Color Black; Down"
|
||||
|
||||
'25, 418 is 0,0
|
||||
global offsetX, offsetY, scaleX, scaleY
|
||||
offsetX = 25: offsetY = 418
|
||||
scaleX = 40: scaleY = 2
|
||||
maxX = 9: maxY = 200
|
||||
|
||||
#gwin "line "; sx( maxX);" "; sy( 0);" "; sx( 0); " "; sy( 0)
|
||||
#gwin "goto "; sx( 0); " "; sy( maxY)
|
||||
|
||||
For x = 0 To 9
|
||||
#gwin "place ";sx(x);" ";sy(0)
|
||||
#gwin "Go -18"
|
||||
#gwin "|"; x
|
||||
Next
|
||||
|
||||
#gwin "turn 90"
|
||||
For y = 0 To 200 Step 20
|
||||
#gwin "place ";sx(0);" ";sy(y)
|
||||
#gwin "Go -5"
|
||||
#gwin "place ";0;" ";sy(y)
|
||||
#gwin "|"; y
|
||||
Next
|
||||
|
||||
#gwin "size 3"
|
||||
For i = 0 To 9
|
||||
#gwin "set ";sx(x(i));" ";sy(y(i))
|
||||
Next i
|
||||
|
||||
#gwin "Flush"
|
||||
Wait
|
||||
|
||||
[quit.gwin]
|
||||
Close #gwin
|
||||
End
|
||||
|
||||
'x, y to screen x, y
|
||||
function sx(x)
|
||||
sx = offsetX +x*scaleX
|
||||
end function
|
||||
|
||||
function sy(y)
|
||||
sy = offsetY-y*scaleY 'y is inverted
|
||||
end function
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
>> x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
|
||||
>> y = [2.7, 2.8, 31.4, 38.1, 58.0, 76.2, 100.5, 130.0, 149.3, 180.0];
|
||||
>> plot(x,y,'.-')
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
x={0,1,2,3,4,5,6,7,8,9};
|
||||
y={2.7,2.8,31.4,38.1,58.0,76.2,100.5,130.0,149.3,180.0};
|
||||
ListPlot[{x, y} // Transpose]
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
(%i1) ".." (m, n) := makelist (i, i, m, n); infix ("..")$
|
||||
(%i2) x: 0 .. 9$ y:[2.7, 2.8, 31.4, 38.1, 58.0, 76.2, 100.5, 130.0, 149.3, 180.0]$
|
||||
(%i3) plot2d(['discrete, x, y], [style, [points,5,1,1]], [gnuplot_term, png], [gnuplot_out_file, "qsort-range-10-9.png"])$
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
#load "graphics.cma"
|
||||
open Graphics
|
||||
|
||||
let round x = int_of_float (floor(x +. 0.5))
|
||||
|
||||
let x = [0; 1; 2; 3; 4; 5; 6; 7; 8; 9]
|
||||
and y = [2.7; 2.8; 31.4; 38.1; 58.0; 76.2; 100.5; 130.0; 149.3; 180.0]
|
||||
|
||||
let () =
|
||||
open_graph "";
|
||||
List.iter2
|
||||
(fun x y ->
|
||||
(* scale to fit in the window *)
|
||||
let _x = x * 30
|
||||
and _y = round(y *. 2.0) in
|
||||
plot _x _y)
|
||||
x y;
|
||||
ignore(wait_next_event [Key_pressed]);
|
||||
close_graph();
|
||||
;;
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
module A = Archimedes
|
||||
|
||||
let y = [|2.7; 2.8; 31.4; 38.1; 58.0; 76.2; 100.5; 130.0; 149.3; 180.0|]
|
||||
|
||||
let () =
|
||||
let vp = A.init [] in
|
||||
A.Axes.box vp;
|
||||
A.set_color vp A.Color.red;
|
||||
A.Array.y vp y;
|
||||
A.close vp
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
|
||||
y = [2.7, 2.8, 31.4, 38.1, 58.0, 76.2, 100.5, 130.0, 149.3, 180.0];
|
||||
plot(x,y,"o");
|
||||
pause;
|
||||
|
|
@ -0,0 +1 @@
|
|||
plothraw(vx, vy)
|
||||
16
Task/Plot-coordinate-pairs/Perl-6/plot-coordinate-pairs.pl6
Normal file
16
Task/Plot-coordinate-pairs/Perl-6/plot-coordinate-pairs.pl6
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
use SVG;
|
||||
use SVG::Plot;
|
||||
|
||||
my @x = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
|
||||
my @y = (2.7, 2.8, 31.4, 38.1, 58.0, 76.2, 100.5, 130.0, 149.3, 180.0);
|
||||
|
||||
my $svg = SVG::Plot.new(
|
||||
width => 512,
|
||||
height => 512,
|
||||
x => @x,
|
||||
x-tick-step => { 1 },
|
||||
values => [@y],
|
||||
title => 'Coordinate Pairs',
|
||||
).plot(:xy-lines);
|
||||
|
||||
say SVG.serialize($svg);
|
||||
15
Task/Plot-coordinate-pairs/Perl/plot-coordinate-pairs-1.pl
Normal file
15
Task/Plot-coordinate-pairs/Perl/plot-coordinate-pairs-1.pl
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
use GD::Graph::points;
|
||||
|
||||
@data = (
|
||||
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
|
||||
[2.7, 2.8, 31.4, 38.1, 58.0, 76.2, 100.5, 130.0, 149.3, 180.0],
|
||||
);
|
||||
$graph = GD::Graph::points->new(400, 300);
|
||||
$gd = $graph->plot(\@data) or die $graph->error;
|
||||
|
||||
# Save as image.
|
||||
$format = $graph->export_format;
|
||||
open(OUF, ">qsort-range-10-9.$format");
|
||||
binmode OUF;
|
||||
print OUF $gd->$format();
|
||||
close(OUF);
|
||||
28
Task/Plot-coordinate-pairs/Perl/plot-coordinate-pairs-2.pl
Normal file
28
Task/Plot-coordinate-pairs/Perl/plot-coordinate-pairs-2.pl
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
use Imager;
|
||||
use Imager::Plot;
|
||||
|
||||
@x = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
|
||||
@y = (2.7, 2.8, 31.4, 38.1, 58.0, 76.2, 100.5, 130.0, 149.3, 180.0);
|
||||
$plot = Imager::Plot->new(
|
||||
Width => 400,
|
||||
Height => 300,
|
||||
GlobalFont => 'PATH_TO_TTF_FONT',
|
||||
);
|
||||
$plot->AddDataSet(
|
||||
X => \@x,
|
||||
Y => \@y,
|
||||
style => {
|
||||
marker => {
|
||||
size => 2,
|
||||
symbol => 'circle',
|
||||
color => Imager::Color->new('red'),
|
||||
},
|
||||
},
|
||||
);
|
||||
$img = Imager->new(
|
||||
xsize => 500,
|
||||
ysize => 400,
|
||||
);
|
||||
$img->box(filled => 1, color => 'white');
|
||||
$plot->Render(Image => $img, Xoff => 50, Yoff => 350);
|
||||
$img->write(file => 'qsort-range-10-9.png');
|
||||
35
Task/Plot-coordinate-pairs/PicoLisp/plot-coordinate-pairs.l
Normal file
35
Task/Plot-coordinate-pairs/PicoLisp/plot-coordinate-pairs.l
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
(load "@lib/ps.l")
|
||||
|
||||
(scl 1)
|
||||
|
||||
(de plot (PsFile DX DY Lst)
|
||||
(let (SX (length Lst) SY (apply max Lst) N 0 Val)
|
||||
(out PsFile
|
||||
(psHead (+ DX 20) (+ DY 40))
|
||||
(font (9 . "Helvetica"))
|
||||
(if (or (=0 SX) (=0 SY))
|
||||
(window 60 12 DX DY
|
||||
(font 24 ,"Not enough Data") )
|
||||
(setq Lst # Build coordinates
|
||||
(let X -1
|
||||
(mapcar
|
||||
'((Y)
|
||||
(cons
|
||||
(*/ (inc 'X) DX SX)
|
||||
(- DY (*/ Y DY SY)) ) )
|
||||
Lst ) ) )
|
||||
(color 55 95 55 # Background color
|
||||
(let (X (+ DX 40) Y (+ DY 40))
|
||||
(poly T 0 0 X 0 X Y 0 Y 0 0) ) )
|
||||
(window 20 20 DX DY # Plot coordinates
|
||||
(poly NIL 0 0 0 DY (- DX 20) DY)
|
||||
(color 76 24 24
|
||||
(poly NIL (caar Lst) (cdar Lst) (cdr Lst)) ) )
|
||||
(window 4 4 60 12 (ps (format SY *Scl)))
|
||||
(for X SX
|
||||
(window (+ 6 (*/ (dec X) DX SX)) (+ 24 DY) 30 12
|
||||
(ps (format (dec X)) 0) ) ) )
|
||||
(page) ) ) )
|
||||
|
||||
(plot "plot.ps" 300 200 (2.7 2.8 31.4 38.1 58.0 76.2 100.5 130.0 149.3 180.0))
|
||||
(call 'display "plot.ps")
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
/x [0 1 2 3 4 5 6 7 8 9] def
|
||||
/y [2.7 2.8 31.4 38.1 58.0 76.2 100.5 130.0 149.3 180.0] def
|
||||
/i 1 def
|
||||
|
||||
newpath
|
||||
x 0 get y 0 get moveto
|
||||
x length 1 sub{
|
||||
x i get y i get lineto
|
||||
/i i 1 add def
|
||||
}repeat
|
||||
stroke
|
||||
|
|
@ -0,0 +1,89 @@
|
|||
Structure PlotData
|
||||
x.i
|
||||
y.f
|
||||
EndStructure
|
||||
|
||||
Global i, x, y.f, max_x, max_y, min_x = #MAXLONG, min_y = Infinity()
|
||||
Define count = (?serie_y - ?serie_x) / SizeOf(Integer) - 1
|
||||
Global Dim MyData.PlotData(count)
|
||||
|
||||
Restore serie_x
|
||||
For i = 0 To count
|
||||
Read.i x
|
||||
MyData(i)\x = x
|
||||
If x > max_x: max_x = x: EndIf
|
||||
If x < min_x: min_x = x: EndIf
|
||||
Next
|
||||
Restore serie_y
|
||||
For i = 0 To count
|
||||
Read.f y
|
||||
MyData(i)\y = y
|
||||
If y > max_y: max_y = y: EndIf
|
||||
If y < min_y: min_y = y: EndIf
|
||||
Next
|
||||
|
||||
Procedure UpdatePlot(Win, w, h)
|
||||
Static gblm = 20, gtrm = 5 ;graph's bottom-left and top-right margin
|
||||
|
||||
Protected count = ArraySize(MyData())
|
||||
If w > gblm And h > gblm And count > 0
|
||||
SetWindowTitle(Win, "PureBasic Plot " + Str(w) + "x" + Str(h))
|
||||
Protected gw = w - gblm, gh = h - gblm ;graph's width and height
|
||||
Protected i, yf.f, xf.f
|
||||
yf = (gh - gtrm) / max_y
|
||||
xf = (gw - gtrm) / max_x
|
||||
|
||||
CreateImage(0, w, h)
|
||||
Protected OutputID = ImageOutput(0)
|
||||
StartDrawing(OutputID)
|
||||
DrawingMode(#PB_2DDrawing_Transparent)
|
||||
;- Draw grid
|
||||
For i = 0 To count
|
||||
y = gh - max_y * i / count * yf
|
||||
LineXY(gblm, y, w - gtrm, y, $467E3E)
|
||||
; Y-scale
|
||||
DrawText(1, y - 5, RSet(StrD(i / count * max_y, 1), 5))
|
||||
x = gblm + max_x * i / count * xf
|
||||
y = gh
|
||||
; X-Scale
|
||||
LineXY(x, y, x, gtrm, $467E3E)
|
||||
If i: DrawText(x - 5, y + 2, Str(i)): EndIf
|
||||
Next
|
||||
|
||||
;- Draw curve
|
||||
Protected ox = gblm, oy = gh, x, y
|
||||
For i = 0 To count
|
||||
x = gblm + MyData(i)\x * xf
|
||||
y = gh - MyData(i)\y * yf
|
||||
LineXY(ox, oy, x, y, $0133EE)
|
||||
ox = x: oy = y
|
||||
Next
|
||||
StopDrawing()
|
||||
ImageGadget(0, 0, 0, w, h, ImageID(0))
|
||||
EndIf
|
||||
EndProcedure
|
||||
|
||||
Define Win = OpenWindow(#PB_Any, 0, 0, 600, 400,"", #PB_Window_SystemMenu | #PB_Window_SizeGadget)
|
||||
If Win
|
||||
SmartWindowRefresh(Win, 1)
|
||||
UpdatePlot(Win, WindowWidth(Win), WindowHeight(Win))
|
||||
Repeat
|
||||
Define event = WaitWindowEvent()
|
||||
Select event
|
||||
Case #PB_Event_SizeWindow
|
||||
UpdatePlot(Win, WindowWidth(Win), WindowHeight(Win))
|
||||
EndSelect
|
||||
Until event = #PB_Event_CloseWindow
|
||||
|
||||
; Save the plot if the user wants to
|
||||
If MessageRequester("Question", "Save it?", #PB_MessageRequester_YesNo) = #PB_MessageRequester_Yes
|
||||
Define File$=SaveFileRequester("Save as", "PB.png", "PNG (*.png)|*.png", 0)
|
||||
UsePNGImageEncoder()
|
||||
SaveImage(0, File$, #PB_ImagePlugin_PNG)
|
||||
EndIf
|
||||
EndIf
|
||||
|
||||
DataSection
|
||||
serie_x: Data.i 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
|
||||
serie_y: Data.f 2.7, 2.8, 31.4, 38.1, 58.0, 76.2, 100.5, 130.0, 149.3, 180.0
|
||||
EndDataSection
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
>>> x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
||||
>>> y = [2.7, 2.8, 31.4, 38.1, 58.0, 76.2, 100.5, 130.0, 149.3, 180.0]
|
||||
|
||||
>>> import pylab
|
||||
>>> pylab.plot(x, y, 'bo')
|
||||
>>> pylab.savefig('qsort-range-10-9.png')
|
||||
2
Task/Plot-coordinate-pairs/R/plot-coordinate-pairs-1.r
Normal file
2
Task/Plot-coordinate-pairs/R/plot-coordinate-pairs-1.r
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
x <- c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
|
||||
y <- c(2.7, 2.8, 31.4, 38.1, 58.0, 76.2, 100.5, 130.0, 149.3, 180.0)
|
||||
1
Task/Plot-coordinate-pairs/R/plot-coordinate-pairs-2.r
Normal file
1
Task/Plot-coordinate-pairs/R/plot-coordinate-pairs-2.r
Normal file
|
|
@ -0,0 +1 @@
|
|||
plot(x,y)
|
||||
2
Task/Plot-coordinate-pairs/R/plot-coordinate-pairs-3.r
Normal file
2
Task/Plot-coordinate-pairs/R/plot-coordinate-pairs-3.r
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
library(lattice)
|
||||
xyplot(y~x)
|
||||
2
Task/Plot-coordinate-pairs/R/plot-coordinate-pairs-4.r
Normal file
2
Task/Plot-coordinate-pairs/R/plot-coordinate-pairs-4.r
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
library(ggplot2)
|
||||
qplot(x,y)
|
||||
12
Task/Plot-coordinate-pairs/Ruby/plot-coordinate-pairs.rb
Normal file
12
Task/Plot-coordinate-pairs/Ruby/plot-coordinate-pairs.rb
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
require 'gnuplot'
|
||||
|
||||
x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
||||
y = [2.7, 2.8, 31.4, 38.1, 58.0, 76.2, 100.5, 130.0, 149.3, 180.0]
|
||||
Gnuplot.open do |gp|
|
||||
Gnuplot::Plot.new( gp ) do |plot|
|
||||
plot.data << Gnuplot::DataSet.new( [x, y] ) do |ds|
|
||||
ds.with = "linespoints"
|
||||
ds.notitle
|
||||
end
|
||||
end
|
||||
end
|
||||
94
Task/Plot-coordinate-pairs/Scala/plot-coordinate-pairs.scala
Normal file
94
Task/Plot-coordinate-pairs/Scala/plot-coordinate-pairs.scala
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
import scala.swing._
|
||||
import scala.swing.Swing._
|
||||
import scala.swing.{MainFrame, Panel, SimpleGUIApplication}
|
||||
import scala.swing.event._
|
||||
import java.awt.{Color, Dimension, Graphics, Graphics2D, Point, geom}
|
||||
|
||||
object PlotCoordPairs extends SimpleSwingApplication {
|
||||
|
||||
//min/max of display-x resp. y
|
||||
val dx0 = 70
|
||||
val dy0 = 30
|
||||
val dxm = 670
|
||||
val dym = 430
|
||||
|
||||
val prefSizeX = 720
|
||||
val prefSizeY = 480
|
||||
|
||||
lazy val ui = new Panel {
|
||||
background = Color.white
|
||||
preferredSize = (prefSizeX, prefSizeY)
|
||||
|
||||
import math._
|
||||
val xmax = {
|
||||
val f1 = pow(10,log10(xs.max).toInt)
|
||||
val f2 = if (f1<10) 10 else round(xs.max/f1)*f1
|
||||
if (f2 >= xs.max) f2 else (round(xs.max/f1)+1)*f1
|
||||
}
|
||||
val ymax = {
|
||||
val f1 = pow(10,log10(ys.max).toInt)
|
||||
val f2 = if (f1<10) 10 else round(ys.max/f1)*f1
|
||||
if (f2 >= ys.max) f2 else (round(ys.max/f1)+1)*f1
|
||||
}
|
||||
val xinterv = xmax/xs.size
|
||||
val yinterv = ymax/xs.size
|
||||
|
||||
case class Coord(x: Double, y: Double) {
|
||||
val dx = (x/xmax*(dxm-dx0)+dx0).toInt
|
||||
val dy = (dym-y/ymax*(dym-dy0)).toInt
|
||||
}
|
||||
|
||||
val pcentre = Coord(0,0)
|
||||
val pxmax = Coord(xmax,0)
|
||||
val pymax = Coord(0, ymax)
|
||||
|
||||
//axes:
|
||||
var a_path = new geom.GeneralPath
|
||||
a_path.moveTo(pcentre.dx, pcentre.dy)
|
||||
a_path.lineTo(pxmax.dx, pxmax.dy) //x-axis
|
||||
a_path.moveTo(pcentre.dx, pcentre.dy)
|
||||
a_path.lineTo(pymax.dx, pymax.dy) //y-axis
|
||||
// interval signs:
|
||||
(0 to xs.size-1).map(i=>Coord(i*xinterv, 0)).map(p=>{a_path.moveTo(p.dx,p.dy);a_path.lineTo(p.dx,p.dy+5)})
|
||||
(0 to xs.size-1).map(i=>Coord(0, i*yinterv)).map(p=>{a_path.moveTo(p.dx,p.dy);a_path.lineTo(p.dx-5,p.dy)})
|
||||
|
||||
//grid:
|
||||
var g_path = new geom.GeneralPath
|
||||
(1 to xs.size).map(i=>Coord(i*xinterv, 0)).map(p=>{g_path.moveTo(p.dx,p.dy);g_path.lineTo(Coord(p.x,ymax).dx,Coord(p.x,ymax).dy)})
|
||||
(1 to xs.size).map(i=>Coord(0, i*yinterv)).map(p=>{g_path.moveTo(p.dx,p.dy);g_path.lineTo(Coord(xmax,p.y).dx,Coord(xmax,p.y).dy)})
|
||||
|
||||
//labeling:
|
||||
val xlabels = (0 to xs.size).map(i=>{val p=Coord(i*xinterv,0); Triple(p.x.toInt.toString,p.dx-3,p.dy+20)})
|
||||
val ylabels = (0 to xs.size).map(i=>{val p=Coord(0,i*yinterv); Triple(p.y.toInt.toString,p.dx-30,p.dy+5)})
|
||||
|
||||
//curve:
|
||||
var path = new geom.GeneralPath
|
||||
val curve = (0 to xs.size-1).map(i=>Coord(xs(i),ys(i)))
|
||||
path.moveTo(curve(0).dx,curve(0).dy)
|
||||
curve.map(p=>path.lineTo(p.dx,p.dy))
|
||||
//...flag all function values:
|
||||
val rects = curve.map(p=>new Rectangle(p.dx-3, p.dy-3, 6, 6))
|
||||
|
||||
override def paintComponent(g: Graphics2D) = {
|
||||
super.paintComponent(g)
|
||||
|
||||
g.setColor(Color.lightGray)
|
||||
g.draw(g_path)
|
||||
g.setColor(Color.black)
|
||||
g.draw(a_path)
|
||||
xlabels.map(t=>g.drawString(t._1,t._2,t._3))
|
||||
ylabels.map(t=>g.drawString(t._1,t._2,t._3))
|
||||
g.draw(path)
|
||||
rects.map(g.draw(_))
|
||||
}
|
||||
}
|
||||
|
||||
val xs = List(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
|
||||
val ys = List(2.7, 2.8, 31.4, 38.1, 58.0, 76.2, 100.5, 130.0, 149.3, 180.0)
|
||||
|
||||
def top = new MainFrame {
|
||||
title = "Rosetta Code >>> Task: Plot coordinate pairs | Language: Scala"
|
||||
contents = ui
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
FnOff
|
||||
PlotsOff
|
||||
NewPlot 1, 1, x, y
|
||||
ZoomData
|
||||
56
Task/Plot-coordinate-pairs/Tcl/plot-coordinate-pairs.tcl
Normal file
56
Task/Plot-coordinate-pairs/Tcl/plot-coordinate-pairs.tcl
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
package require Tk
|
||||
|
||||
# The actual plotting engine
|
||||
proc plotxy {canvas xs ys} {
|
||||
global xfac yfac
|
||||
set maxx [tcl::mathfunc::max {*}$xs]
|
||||
set maxy [tcl::mathfunc::max {*}$ys]
|
||||
set xfac [expr {[winfo width $canvas] * 0.8/$maxx}]
|
||||
set yfac [expr {[winfo height $canvas] * 0.8/$maxy}]
|
||||
scale $canvas x 0 $maxx $xfac
|
||||
scale $canvas y 0 $maxy $yfac
|
||||
foreach x $xs y $ys {
|
||||
dot $canvas [expr {$x*$xfac}] [expr {$y*$yfac}] -fill red
|
||||
}
|
||||
}
|
||||
# Rescales the contents of the given canvas
|
||||
proc scale {canvas direction from to fac} {
|
||||
set f [expr {$from*$fac}]
|
||||
set t [expr {$to*$fac}]
|
||||
switch -- $direction {
|
||||
x {
|
||||
set f [expr {$from * $fac}]
|
||||
set t [expr {$to * $fac}]
|
||||
$canvas create line $f 0 $t 0
|
||||
$canvas create text $f 0 -anchor nw -text $from
|
||||
$canvas create text $t 0 -anchor n -text $to
|
||||
|
||||
}
|
||||
y {
|
||||
set f [expr {$from * -$fac}]
|
||||
set t [expr {$to * -$fac}]
|
||||
$canvas create line 0 $f 0 $t
|
||||
$canvas create text 0 $f -anchor se -text $from
|
||||
$canvas create text 0 $t -anchor e -text $to
|
||||
}
|
||||
}
|
||||
}
|
||||
# Helper to make points, which are otherwise not a native item type
|
||||
proc dot {canvas x y args} {
|
||||
set id [$canvas create oval [expr {$x-3}] [expr {-$y-3}] \
|
||||
[expr {$x+3}] [expr {-$y+3}]]
|
||||
$canvas itemconfigure $id {*}$args
|
||||
}
|
||||
|
||||
pack [canvas .c -background white]
|
||||
update
|
||||
set xs {0 1 2 3 4 5 6 7 8 9}
|
||||
set ys {2.7 2.8 31.4 38.1 58.0 76.2 100.5 130.0 149.3 180.0}
|
||||
plotxy .c $xs $ys
|
||||
.c config -scrollregion [.c bbox all]
|
||||
.c move all 20 20
|
||||
|
||||
# Save image (this is the only part that requires an external library)
|
||||
package require Img
|
||||
set im [image create photo -data .c]
|
||||
$im write plotxy.png -format PNG
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
#import std
|
||||
#import flo
|
||||
#import fit
|
||||
#import plo
|
||||
|
||||
x = <0., 1., 2., 3., 4., 5., 6., 7., 8., 9.>
|
||||
y = <2.7, 2.8, 31.4, 38.1, 58.0, 76.2, 100.5, 130.0, 149.3, 180.0>
|
||||
|
||||
#output dot'tex' latex_document+ plot
|
||||
|
||||
main = visualization[curves: <curve[points: ~&p/x y]>]
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
main =
|
||||
|
||||
visualization[
|
||||
abscissa: axis[
|
||||
variable: 'problem size',
|
||||
hats: printf/*'%0.0f' ari10/0. 9.],
|
||||
ordinates: ~&iNC axis[
|
||||
variable: 'execution time ($\mu$s)',
|
||||
hats: printf/*'%0.1f' ari6/0. 180.],
|
||||
curves: <
|
||||
curve[
|
||||
points: ^(~&,chord_fit0@p/x y)* ari200/0. 9.,
|
||||
attributes: {'linecolor': 'lightgray'}],
|
||||
curve[
|
||||
scattered: true,
|
||||
points: ~&p/x y,
|
||||
attributes: {'linecolor': 'black'}]>]
|
||||
34
Task/Plot-coordinate-pairs/XPL0/plot-coordinate-pairs.xpl0
Normal file
34
Task/Plot-coordinate-pairs/XPL0/plot-coordinate-pairs.xpl0
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
include c:\cxpl\codes; \intrinsic 'code' declarations
|
||||
|
||||
def ScrW=640, ScrH=480, VidMode=$101;
|
||||
def Sx = ScrW/10, \pixels per horz grid line
|
||||
Sy = ScrH/10, \pixels per vert grid line
|
||||
Ox = (3+1+1)*8+2, \offset for horz grid: allow room for "180.0"
|
||||
Oy = ScrH-20; \offset for vert grid: allow room for labels
|
||||
int X, DataX;
|
||||
real Y, DataY, Gain;
|
||||
def Brown=6, LCyan=11;
|
||||
|
||||
[DataX:= [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
|
||||
DataY:= [2.7, 2.8, 31.4, 38.1, 58.0, 76.2, 100.5, 130.0, 149.3, 180.0];
|
||||
|
||||
SetVid(VidMode);
|
||||
for X:= 0 to 9 do \draw grid
|
||||
[Move(X*Sx+Ox, Oy); Line(X*Sx+Ox, Oy-9*Sy, Brown); \vert lines
|
||||
Move(Ox, Oy-X*Sy); Line(9*Sx+Ox, Oy-X*Sy, Brown); \horz lines
|
||||
];
|
||||
Format(3,1); Attrib(LCyan); \label grid
|
||||
Y:= 0.0;
|
||||
for X:= 0 to 9 do
|
||||
[Move(X*Sx+Ox-3, Oy+6); IntOut(6, X); \X axis
|
||||
Move(0, Oy-X*Sy-7); RlOut(6, Y); \Y axis
|
||||
Y:= Y + 20.0;
|
||||
];
|
||||
Gain:= float(Sy)/20.0;
|
||||
Move(DataX(0)*Sx+Ox, Oy-Fix(DataY(0)*Gain)); \plot points
|
||||
for X:= 1 to 9 do
|
||||
Line(DataX(X)*Sx+Ox, Oy-Fix(DataY(X)*Gain), LCyan);
|
||||
|
||||
if ChIn(1) then []; \wait for key
|
||||
SetVid(3); \restore text
|
||||
]
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
|
||||
y = [2.7, 2.8, 31.4, 38.1, 58.0, 76.2, 100.5, 130.0, 149.3, 180.0];
|
||||
window, 0;
|
||||
plmk, y, x;
|
||||
window, 1;
|
||||
plg, y, x, marks=0;
|
||||
Loading…
Add table
Add a link
Reference in a new issue