Scott Rossi <[EMAIL PROTECTED]> wrote:

> Subject: Math Challenge
> Date: Thu, 08 Mar 2001 03:31:50 -0800
<snip>
> I'm wondering if someone good at geometric math might be able to figure out
> the following:  how to drag an object relative to the points of a
> non-vertical ellipse (an oval which is rotated by some odd measurement).

An interesting diversion.  Below are two handlers:  drawE, mouseDown.  Make
a new stack (default size OK) and put these into stack script, removing any
line breaks added by email program.  Run draw E. Then click and drag your
mouse.  Y direction pointing down kept, so angles run CW from positive x
axis.  Scripts not optimized or double-checked, and seems to be an error in
exact location of "dot".

Rich Herz <ucsd.edu>

on drawE
  global theta, a, b, yO, xO
  
  # positive y and yp directions point down on screen
  # so positive angle is rotation CW, rather than standard CCW when y
direction is up
  # ellipse dimensions a & b are defined with respect to xp, yp axes
  # xp, yp axes are rotated theta degrees from screen x,y axis
  
  # ADJUSTABLE PARAMETERS:
  put 30 into theta # (<= 90) angle xp,yp axis is rotated from x,y axis
  put 50 into a # intersection of ellipse with xp axis
  put 30 into b # intersection of ellipse with yp axis
  put 150 into yO  # y Offset of ellipse origin from window y zero
  put 150 into xO  # x Offset of ellipse origin from window x zero
  
  put pi*theta/180 into thetaRad  # thetaRad in radians
  
  repeat with phi = 1 to 360  # phi in degrees
    
    put pi*phi/180 into phiRad  # phiRad in radians
    
    put a*cos(phiRad) into xp
    put b*sin(phiRad) into yp
    
    # convert xp, yp of rotated ellipse coordinates into x,y screen coord.
    put cos(thetaRad)*(xp - yp*cos(thetaRad)) into x
    put yp/cos(thetaRad) + tan(thetaRad)*x into y
    
    add xO to x
    add yO to y
    put round(x), round(y) into line phi of thePoints
    
  end repeat
  
  if there is not a graphic "ellipse" then
    create graphic "ellipse"
    set the style of graphic "ellipse" to "polygon"
  end if
  
  if there is not a graphic "dot" then
    create graphic "dot"
    set the style of graphic "dot" to "oval"
    set the width of graphic "dot" to 10
    set the height of graphic "dot" to 10
  end if
  
  set the points of graphic "ellipse" to  thePoints
  set the loc of graphic "dot" to xO, yO
  
end drawE

# ---------------------

on mouseDown
  global theta, a, b, yO, xO
  
  repeat while the mouse is down
    
    put the mouseLoc into xy
    put item 1 of xy into x
    put item 2 of xy into y
    subtract xO from x
    subtract yO from y
    if x = 0 then add 1 to x # to prevent divide by zero in atan(y/x) below
    
    put atan(y/x) into sumThetaPhi # sumThetaPhi in radians
    put sumThetaPhi*180/pi into sumThetaPhi # sumThetaPhi in degrees
    
    #   sumThetaPhi now ranges between +/- 90 degrees from nearest x axis
    # convert to 0-360 range from positive x axis starting in positive y
direction (down, so CW here)
    # first get quadrant, then handle separately for each quadrant (here,
positive y is down)

    # xxx this may be too complex, just what worked first! xxx
    
    # get quadrant
    if x < 0 and y < 0 then
      put 3 into quad
    else  if x < 0 and y >= 0 then
      put 2 into quad
    else  if x >= 0 and y < 0 then
      put 4 into quad
    else
      put 1 into quad
    end if
    
    switch quad
    case 1
      if sumThetaPhi <= theta then
        put sumThetaPhi - theta into phi
      else
        put 360 - theta + sumThetaPhi into phi
      end if
      break
    case 2
      if sumThetaPhi < 0 then
        put 180 + sumThetaPhi into sumThetaPhi
        put sumThetaPhi - theta into phi
        break
      case 3
        put 180 + sumThetaPhi into sumThetaPhi
        put sumThetaPhi - theta into phi
        break
      case 4
        put 360 + sumThetaPhi into sumThetaPhi
        put sumThetaPhi - theta into phi
        break
      end switch
      
      # phi is angle in degrees with respect to xp,yp coordinates of line
between mouse & origin
      
      # xxx except something appears to be off somewhere in dot location xxx
      
      put pi*phi/180 into phiRad # phiRad in radians
      put a*cos(phiRad) into xp
      put b*sin(phiRad) into yp
      # x,y are coordinates in xp,yp axis of intersection between ellipse
and line between mouse & origin
      
      put pi*theta/180 into thetaRad
      put cos(thetaRad)*(xp - yp*cos(thetaRad)) into x
      put yp/cos(thetaRad) + tan(thetaRad)*x into y
      # x,y are coordinates in x,y axis of intersection between ellipse and
line between mouse & origin
      
      add xO to x
      add yO to y
      set the loc of graphic "dot" to round(x), round(y)
      
    end repeat
    
  end mouseDown


Archives: http://www.mail-archive.com/[email protected]/
Info: http://www.xworlds.com/metacard/mailinglist.htm
Please send bug reports to <[EMAIL PROTECTED]>, not this list.

Reply via email to