example code
- Posted by Matthew McNamara <mat at ICONZ.CO.NZ> Aug 13, 1998
- 564 views
Someone may find this code useful, please send me a comment if you can think of a way to improve it. I use it (among other functions) to draw scalable text on graphs. -- routine: CURVE -- version: 1 -- filename: CURVE.E -- language: Euphoria version 1.5 -- operating system: tested on MS-DOS version 6.22 -- hardware: IBM PC -- author: Matthew McNamara <mat at iconz.co.nz> -- short description: map 1/4 of a circle to any 2 points -- created: 1998-08-11 include graphics.e --for test code include get.e --for test code constant PI=3.1415926536 global procedure curve(integer pen, object P1, object P3, object P2) -- P1, end point, 2-atom sequence, {xpixel,ypixel} -- P3, curving point, 2-atom sequence, {xpixel,ypixel} -- P2, end point, 2-atom sequence, {xpixel,ypixel} -- -- example (note angle P1 P3 P2 need not be a right angle) -- P1 -- . -- . -- -- . -- -- . -- . -- P3 . .P2 object temp atom y, steps, P1P3_len, P2P3_len sequence P3P1, P2P3 temp = P1-P3 temp = temp*temp P1P3_len = sqrt(temp[1] + temp[2]) temp = P2-P3 temp = temp*temp P2P3_len = sqrt(temp[1] + temp[2]) steps = P1P3_len + P2P3_len --guesstimate to avoid large gaps P3P1 = P3-P1 P2P3 = P2-P3 if steps = 0 then return end if for x = 0 to cos(PI/4) by 1/steps do y = 1 - sqrt(1 - x*x) pixel(pen, P1 + P3P1*x + P2P3*y) pixel(pen, P1 + P3P1*(1-y) + P2P3*(1-x)) --mirror end for end procedure --test code --generate random curves between 3 colored dots --esc to exit, any other key for a new curve object ignore, video, x, y, a, b, c ignore = graphics_mode(18) ignore = { palette(1, {63,63,63}), palette(2, {63,00,00}), palette(3, {63,63,00}), palette(4, {00,00,63}) } video = video_config() x = video[VC_XPIXELS] y = video[VC_YPIXELS] while 1 do a = {rand(x)-1, rand(y)-1} b = {rand(x)-1, rand(y)-1} c = {rand(x)-1, rand(y)-1} clear_screen() ellipse(2, 1, a-5, a+5) ellipse(4, 1, c-5, c+5) ellipse(3, 1, b-5, b+5) curve(1, a, b, c) ignore = wait_key() if ignore = 27 then exit end if end while ignore = graphics_mode(-1) -- end of test code Matthew McNamara _ mat at iconz.co.nz o( ) The Internet Company of New Zealand / /\