Re: [racket-users] Re: Arcs and lines being half-drawn

2015-03-26 Thread chia kang ren
http://pasterack.org/pastes/75790
http://pasterack.org/pastes/11360
http://pasterack.org/pastes/10455

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[racket-users] Re: Arcs and lines being half-drawn

2015-03-26 Thread chia kang ren
Here is a clearer photo. In the original (and working) graphical editor you can 
select the entities to highlight them in red. The entities are weirdly cut off. 
The top left arc is cut off in the middle! Have since commented out (define 
draw-line ..), but the problem still persists.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket] newb: implementation of select items

2015-01-24 Thread Chia Kang Ren
I have been working on and off on this graphical editor, and i seem to come 
across a recurring problem that my initial ideas for the program design weren’t 
sound, and i have to go back one or two things. This affects the rest of the 
system and forces me to modify a lot other functions. One example is inside 
(define (open-file … ) where i now use search-list instead of struct-list (this 
is a quick hack to get the program working, which i will go back and change 
eventually) because i realized i need to scale the values. 

Another thing is how to break up my code. I have read the style documentation 
here  and forgive me but 
i am new to software development, and i still don’t know what constitutes good 
modularity in a program. I do have defined sections in this one big script 
though, namely txt parsing, auxilliary functions, geometric functions, 
intersection query functions, but i am afraid they are blurring the lines and 
it is getting harder for me to tell which functions go where.

Finally, i wish to say thanks for making Racket such an enjoyable programming 
environment. DrRacket made it very easy for me to started and the ease of 
creating a GUI environment quickly was what drew me to start my project here.

P.S. no global variables in caps now :)

http://pasterack.org/pastes/70507 

> On 22 Dec 2014, at 6:42 pm, Neil Van Dyke  wrote:
> 
> I think you have some of the right ideas.
> 
> Two additional ideas to consider...
> 
> * Usually you will have some data representation of objects that will be 
> drawn and manipulated.  This might be as simple as a list of structs or 
> Racket objects, and (in an OOPL way).  If you're making a simple graphical 
> editor, these might have objects like `editor-circle`, which knows its 
> position, size, and color, as well as has methods like "draw this object on 
> given graphic context" and "does this object intersect with given selection 
> rectangle?".  The coordinates for these objects will probably be in a "world" 
> coordinate system, which stays the same regardless of whatever scaling, 
> scrolling, rotating, etc. is going on in your editor.  One of the many 
> implications of the methods, to answer one of your questions, is that, when 
> you are dragging out your selection rectangle, you can just traverse this 
> list, asking each object whether it intersects with the rectangle.  (If your 
> editor gets more sophisticated, or has to deal with , or you can get fancier 
> with your data structures, which would include making the selection more 
> efficient than O(n).)
> 
> * When you are looking at doing drawing that is expensive, such as 
> highlighting objects as you drag the selection rectangle over them, consider 
> how using off-screen buffers can make that computationally easier.  You might 
> have heard of "double-buffering" as a technique for avoiding flicker in 
> animations, but you can use similar techniques for things like capturing a 
> baseline drawing state and then doing fast incremental changes atop that 
> (say, for highlighting objects during a select rectangle drag, or for 
> dragging selected objects).
> 
> Also, I beg people not to use all-caps for constants.  All-caps are too 
> useful for syntax transformer variables (which is also a use more consistent 
> with the original reason that people started using all-caps for constants, 
> which wasn't because they were constants but because they were involved in... 
> syntax transformation).  And, anyway, more descriptive than `WIDTH` is 
> `editor-frame-width`.
> 
> Neil V.
> 


  Racket Users list:
  http://lists.racket-lang.org/users


[racket] newb: implementation of select items

2014-12-22 Thread chia kang ren
I wish to draw onto a canvas, select certain objects using a (repaint
the selected objects red) self-implemented select method, and delete
them (undo if done wrongly) with, small misc. features like zooming in
and out, dragging the canvas to view different areas. I've gotten so
far as mimicking a graphical representation of "selecting", but it
seems awfully laggy. Second, the bounding box of "select" disappears
if the user holds the mouse cursor inert. I can remedy this by placing
the line drawing functions in paint-callback, but i don't know if that
would be a good idea to do it with a flag (to differentiate between
normal drawing and redrawing selected items) Also, implementing a
select method seems way more complex than i first thought it would be,
although since now i'm at this point, i can roughly figure out the
next few steps would be.


1. A function to calculate all the bounding areas of currently-drawn
objects, then redraw them in red if their coordinates are within the
bounding box of "select".

2. Redraw all objects without selected items if delete button pressed.


If i am unnecessarily doing anything or looking at the problem the
wrong way, please let me know. Thanks!


#lang racket/gui

(define WIDTH 800)
(define HEIGHT 600)
(define GLOBAL-X-OFFSET 200)
(define GLOBAL-Y-OFFSET 450)
(define X-SCALE 1)
(define Y-SCALE -1)
(define TRANSFORMATION-MATRIX (vector 1 0 0 1 0 0))
(define ROTATION 0)
(define init-x 0)
(define init-y 0)
(define top-frame (new frame%
 [label "KR"]
 [width WIDTH]
 [height HEIGHT]
 [alignment (list 'left 'top)]))

(define my-canvas%
  (class canvas% ; The base class is canvas%
;; Declare overrides:
(override on-char)
;; Define overriding method to handle mouse events
(define on-char (lambda (event)
  (let ((key (send event get-key-code)))
(case key
  ['wheel-up(set! X-SCALE (+ X-SCALE 0.1))
(set! Y-SCALE (- Y-SCALE 0.1))]
  ['wheel-down  (set! X-SCALE (- X-SCALE 0.1))
(set! Y-SCALE (+ Y-SCALE 0.1))])
  (send drawer set-transformation (vector
TRANSFORMATION-MATRIX GLOBAL-X-OFFSET GLOBAL-Y-OFFSET X-SCALE Y-SCALE
ROTATION))
  (send canvas refresh-now
;; Call the superclass initialization (and pass on all init args)
(define/override (on-event event)
(define x (send event get-x))
(define y (send event get-y))
(cond
  ((and (send event button-down? 'left) (send event get-control-down))
   (set! init-x x)
   (set! init-y y)
   (display (list y (- init-y GLOBAL-Y-OFFSET
  ((and (send event button-up? 'left) (send event get-control-down)))
   ;(send drawer draw-line init-x 0 x 0))
  ((and (send event dragging?) (send event get-control-down))
   (let ((start-x  (/ (- init-x GLOBAL-X-OFFSET) X-SCALE))
 (start-y  (/ (- init-y GLOBAL-Y-OFFSET) Y-SCALE))
 (current-x(/ (- x GLOBAL-X-OFFSET) X-SCALE))
 (current-y(/ (- y GLOBAL-Y-OFFSET) Y-SCALE)))
 (send drawer draw-line start-x start-y current-x start-y)
 (send drawer draw-line current-x start-y current-x current-y)
 (send drawer draw-line current-x current-y start-x current-y)
 (send drawer draw-line start-x current-y start-x start-y)))
  ((send event button-down? 'left)
   (set! init-x x)
   (set! init-y y))
  ((send event button-up? 'left)
   (set! GLOBAL-X-OFFSET (vector-ref (send drawer
get-transformation) 1))
   (set! GLOBAL-Y-OFFSET (vector-ref (send drawer
get-transformation) 2)))
  ((send event dragging?)
   (let* ((current-x (- x init-x))
  (current-y (- y init-y)))
 (send drawer set-transformation (vector
TRANSFORMATION-MATRIX (+ current-x GLOBAL-X-OFFSET) (+ current-y
GLOBAL-Y-OFFSET) X-SCALE Y-SCALE ROTATION))
 (send canvas refresh-now)
(super-instantiate (

(new button%
 [label "Select"]
 [parent top-frame])
(new button%
 [label "Select"]
 [parent top-frame])

(define canvas (new my-canvas%
[parent top-frame]
;[style (list 'hscroll 'vscroll 'resize-corner)]
[paint-callback (lambda (canvas dc)
  (define no-brush (new brush%
[style 'transparent]))
  (send drawer set-brush no-brush)
  (send drawer draw-line 0 0 200 200)
  ;(send drawer draw-line 0 0 0 200)
  ;(send drawer draw-line 0 200 200 200)
  ;(send drawer draw-line 200 200 200 0)