coordinate-transformation
Posted: Thu Jan 05, 2017 1:17 pm
Hi Folks,
having appreciated so much the work of Henko, that one can barely keep up with
, here my modest first contribution to the library.
It's about coordinate-transformation.
The functions help to transform coordinates from a user-defined coordinate-system to the ipad screen & vice versa - simple but efficient.
Please note that the code contains three sections.
1. section contains a description of all functions
2. section includes a snippet which demonstrates most of the functions - just remove the /*...*/
3. section includes the functions themselves
Hope it is helpful for you...bye, Joel
having appreciated so much the work of Henko, that one can barely keep up with
![Wink ;-)](./images/smilies/icon_e_wink.gif)
It's about coordinate-transformation.
The functions help to transform coordinates from a user-defined coordinate-system to the ipad screen & vice versa - simple but efficient.
Please note that the code contains three sections.
1. section contains a description of all functions
2. section includes a snippet which demonstrates most of the functions - just remove the /*...*/
3. section includes the functions themselves
Hope it is helpful for you...bye, Joel
Code: Select all
/*
'c'-description
'y'-testing
'b'-definitions
''
'c'
returns the "x"- and "y"-coordinate of a point on the ipad-screen. Basis is a user-defined coordinate-system given by xmin, xmax, ymin, ymax
===========
global Variables
===========
xmin:smallest number of x-range within new coordinate system
xmax:biggest number of x-range
ymin:smallest number of y-range
ymax:biggest number of y-range
by omitting one pair (e.g.:ymin,ymax) those values are then calculated with respect to screen ratio
==========
functions
==========
tc_x(x):
transforms "x"-user-coordinate into "x"-coordinate of ipad-screen. usable with DRAW LINE etc.
When using PIXEL then call function as:2*tc_x(x) etc.
"sizes" within user-defined system like "width" or "height" must be treated like :
SIZE tc_x(10)-tc_x(0) which corresponds to SIZE 10
x: "x"-coordinate within user-defined coordinate-system
----------
btc_x(x):
inverse function of tc_x(x).
returns the "x"-coordinate of the user-defined coordinate-system. Basis is the corresponding point on ipad-screen. e.g. usable when transforming touch-information into user-defined -coordinates
x: x-value of the screen
----------
tc_y(y):
analogue
----------
btc_y(y):
analogue
----------
draw_sys():
draws the two axis of the user-defined coordinate-system
----------
draw_ticks(tick_x,tick_y):
draws ticks in a user-defined coordinate-system
tick_x: x-value of interval for each tick
tick_y: analogue
----------
draw_grid(dx,dy):
draws a grid in a defined coordinate-system
dx: x-value of interval
dy: analogue
----------
get_unit_x():
returns the smallest element on x-scale in respect of displayable points and params xmin, xmax
(note: DRAW-command etc. NOT PIXEL-command
----------
get_unit_y():
analogue
----------
get_ratio():
returns ratio: dy/dx
''
*/
/*
'y'
'testing
sw=SCREEN_WIDTH() ! sh=SCREEN_HEIGHT()
pi=3.14159
OPTION ANGLE RADIANS
GRAPHICS CLEAR 1,1,1
GRAPHICS
'define boundaries of coordinate-system via global variables
xmin=-pi
xmax=pi
ymin=-50
ymax=50
'draw grid
DRAW COLOR 0.9,0.8,1
DRAW SIZE 1
draw_grid(pi/4,10)
'draw axis
DRAW SIZE 3
DRAW DASH 10
DRAW COLOR 0,0,0
draw_sys
'graph
DRAW SIZE 5
DRAW COLOR 1,0,0
DRAW TO tc_x(xmin),tc_y(0)
FOR phi= xmin TO xmax STEP .1
DRAW LINE TO tc_x(phi),tc_y(10*SIN(phi))
NEXT phi
draw_ticks(pi/2,10)
'set up touch output field
FIELD "touch_output" TEXT "touch x:= "&CHR$(10)&"touch y:= " AT sw*0,sh*0.9 SIZE sw,sh*.1 ML
'touch
LOOP:
SLOWDOWN
GET TOUCH o AS touchx, touchy
'transform ipad-coordinates into user-defined coordinates
bt_touchx=btc_x(touchx) ! bt_touchy=btc_y(touchy)
'output back-transformed touch-coordinates
IF touchx>0 AND touchy>0 AND bt_touchx>=0 THEN FIELD "touch_output" TEXT "touch x:= "&LEFT$(STR$(bt_touchx/3.1415),4)&"*"&CHR$(960)&CHR$(10) & "touch y:= "&bt_touchy
IF touchx>0 AND touchy>0 AND bt_touchx<0 THEN FIELD "touch_output" TEXT "touch x:= "&LEFT$(STR$(bt_touchx/3.1415),5)&"*"&CHR$(960)&CHR$(10) & "touch y:= "&bt_touchy
GOTO LOOP
''
*/
'b'
DEF tc_x(x)
'calculate min& max values when those are left to zero respecting screen-ratio
IF .ymin=0 AND .ymax=0 THEN
.ymin=.xmin*SCREEN_HEIGHT()/SCREEN_WIDTH() ! .ymax=.xmax*SCREEN_HEIGHT()/SCREEN_WIDTH()
ENDIF
IF .xmin=0 AND .xmax=0 THEN
.xmin=.ymin*SCREEN_WIDTH()/SCREEN_HEIGHT() ! .xmax=.ymax*SCREEN_WIDTH()/SCREEN_HEIGHT()
ENDIF
'calculate gradient, linear transormation function
mx=SCREEN_WIDTH() /(.xmax-.xmin)
x0=(-1)*.xmin*mx
tc_x=x*mx+x0
END DEF
'=================
DEF tc_y(y)
IF .ymin=0 AND .ymax=0 THEN
.ymin=.xmin*SCREEN_HEIGHT()/SCREEN_WIDTH() ! .ymax=.xmax*SCREEN_HEIGHT()/SCREEN_WIDTH()
ENDIF
IF .xmin=0 AND .xmax=0 THEN
.xmin=.ymin*SCREEN_WIDTH()/SCREEN_HEIGHT() ! .xmax=.ymax*SCREEN_WIDTH()/SCREEN_HEIGHT()
ENDIF
my=SCREEN_HEIGHT() / (.ymin-.ymax)
y0=(-1)*.ymax*my
tc_y=y*my+y0
END DEF
'=================
DEF btc_x(x)
'back-transformation
IF .ymin=0 AND .ymax=0 THEN
.ymin=.xmin*SCREEN_HEIGHT()/SCREEN_WIDTH() ! .ymax=.xmax*SCREEN_HEIGHT()/SCREEN_WIDTH()
ENDIF
IF .xmin=0 AND .xmax=0 THEN
.xmin=.ymin*SCREEN_WIDTH()/SCREEN_HEIGHT() ! .xmax=.ymax*SCREEN_WIDTH()/SCREEN_HEIGHT()
ENDIF
mx=SCREEN_WIDTH() /(.xmax-.xmin)
x0=(-1)*.xmin*mx
btc_x=(x-x0)/mx
END DEF
'=================
DEF btc_y(y)
IF .ymin=0 AND .ymax=0 THEN
.ymin=.xmin*SCREEN_HEIGHT()/SCREEN_WIDTH() ! .ymax=.xmax*SCREEN_HEIGHT()/SCREEN_WIDTH()
ENDIF
IF .xmin=0 AND .xmax=0 THEN
.xmin=.ymin*SCREEN_WIDTH()/SCREEN_HEIGHT() ! .xmax=.ymax*SCREEN_WIDTH()/SCREEN_HEIGHT()
ENDIF
my=SCREEN_HEIGHT() / (.ymin-.ymax)
y0=(-1)*.ymax*my
btc_y=(y-y0)/my
END DEF
'=================
DEF draw_sys()
IF .ymin=0 AND .ymax=0 THEN
.ymin=.xmin*SCREEN_HEIGHT()/SCREEN_WIDTH() ! .ymax=.xmax*SCREEN_HEIGHT()/SCREEN_WIDTH()
ENDIF
IF .xmin=0 AND .xmax=0 THEN
.xmin=.ymin*SCREEN_WIDTH()/SCREEN_HEIGHT() ! .xmax=.ymax*SCREEN_WIDTH()/SCREEN_HEIGHT()
ENDIF
DRAW LINE tc_x(.xmin),tc_y(0) TO tc_x(.xmax),tc_y(0)
DRAW LINE tc_x(0),tc_y(.ymin) TO tc_x(0),tc_y(.ymax)
END DEF
'=================
DEF draw_ticks(tick_x,tick_y)
IF .ymin=0 AND .ymax=0 THEN
.ymin=.xmin*SCREEN_HEIGHT()/SCREEN_WIDTH() ! .ymax=.xmax*SCREEN_HEIGHT()/SCREEN_WIDTH()
ENDIF
IF .xmin=0 AND .xmax=0 THEN
.xmin=.ymin*SCREEN_WIDTH()/SCREEN_HEIGHT() ! .xmax=.ymax*SCREEN_WIDTH()/SCREEN_HEIGHT()
ENDIF
FOR n=tick_x TO .xmax STEP tick_x
DRAW LINE tc_x(n),tc_y(0)-5 TO tc_x(n), tc_y(0)+5
NEXT n
FOR n= -tick_x TO .xmin STEP -tick_x
DRAW LINE tc_x(n),tc_y(0)-5 TO tc_x(n), tc_y(0)+5
NEXT n
FOR n=tick_y TO .yMAX STEP tick_y
DRAW LINE tc_x(0)-5,tc_y(n) TO tc_x(0)+5, tc_y(n)
NEXT n
FOR n=-tick_y TO .ymin STEP -tick_y
DRAW LINE tc_x(0)-5,tc_y(n) TO tc_x(0)+5, tc_y(n)
NEXT n
END DEF
'=================
DEF draw_grid(d_x, d_y)
IF .ymin=0 AND .ymax=0 THEN
.ymin=.xmin*SCREEN_HEIGHT()/SCREEN_WIDTH() ! .ymax=.xmax*SCREEN_HEIGHT()/SCREEN_WIDTH()
ENDIF
IF .xmin=0 AND .xmax=0 THEN
.xmin=.ymin*SCREEN_WIDTH()/SCREEN_HEIGHT() ! .xmax=.ymax*SCREEN_WIDTH()/SCREEN_HEIGHT()
ENDIF
FOR x=d_x TO .xmax STEP d_x
DRAW LINE tc_x(x),tc_y(.ymin) TO tc_x(x), tc_y(.ymax)
NEXT x
FOR x=d_x TO .xmin STEP -d_x
DRAW LINE tc_x(x),tc_y(.ymin) TO tc_x(x), tc_y(.ymax)
NEXT x
FOR y=d_y TO .ymax STEP d_y
DRAW LINE tc_x(.xmin),tc_y(y) TO tc_x(.xmax),tc_y(y)
NEXT y
FOR y=d_y TO .ymin STEP -d_y
DRAW LINE tc_x(.xmin),tc_y(y) TO tc_x(.xmax),tc_y(y)
NEXT y
END DEF
'=================
DEF get_unit_x
IF .ymin=0 AND .ymax=0 THEN
.ymin=.xmin*SCREEN_HEIGHT()/SCREEN_WIDTH() ! .ymax=.xmax*SCREEN_HEIGHT()/SCREEN_WIDTH()
ENDIF
IF .xmin=0 AND .xmax=0 THEN
.xmin=.ymin*SCREEN_WIDTH()/SCREEN_HEIGHT() ! .xmax=.ymax*SCREEN_WIDTH()/SCREEN_HEIGHT()
ENDIF
get_unit_x= (.xmax-.xmin)/SCREEN_WIDTH()
END DEF
'=================
DEF get_unit_y
IF .ymin=0 AND .ymax=0 THEN
.ymin=.xmin*SCREEN_HEIGHT()/SCREEN_WIDTH() ! .ymax=.xmax*SCREEN_HEIGHT()/SCREEN_WIDTH()
ENDIF
IF .xmin=0 AND .xmax=0 THEN
.xmin=.ymin*SCREEN_WIDTH()/SCREEN_HEIGHT() ! .xmax=.ymax*SCREEN_WIDTH()/SCREEN_HEIGHT()
ENDIF
get_unit_y= (.ymax-.ymin)/SCREEN_HEIGHT()
END DEF
'=================
DEF get_ratio
IF .ymin=0 AND .ymax=0 THEN
.ymin=.xmin*SCREEN_HEIGHT()/SCREEN_WIDTH() ! .ymax=.xmax*SCREEN_HEIGHT()/SCREEN_WIDTH()
ENDIF
IF .xmin=0 AND .xmax=0 THEN
.xmin=.ymin*SCREEN_WIDTH()/SCREEN_HEIGHT() ! .xmax=.ymax*SCREEN_WIDTH()/SCREEN_HEIGHT()
ENDIF
get_ratio= (.ymax-.ymin)/(.xmax-.xmin)
END DEF