1

I would like to draw rotated ellipse, centered at origin I know how to do it in R and the theory

My algorithm:

  • compute a list of points paramterized by t
  • each point is rotated by angle theta

My code:



deg2rad(t):= float(t*2*%pi/360)$

GiveRotatedEllipse(a,b,theta, NumberOfPoints):=block(
    [x, y, zz, t , tmin, tmax, dt, c, s],
    zz:[],
    dt : 1/NumberOfPoints, 
    tmin: 0, 
    tmax: 2*%pi,
    c:float(cos(theta)),
    s:float(sin(theta)),
    for t:tmin thru tmax step dt do(
        x: a*cos(t)*c - b*sin(t)*s,
        x: float(x), 
        y: a*cos(t)*c + b*sin(t)*c,
        y:float(y),
        zz: cons([x,y],zz)
    ),
    return (points(zz))
)$



/* compute */

/* angles fo trigonometric functions in radians */
angle_step :deg2rad(15) $  /* 2*%pi/3$   */
radius_x: 3$
radius_y: 2$




e0:GiveRotatedEllipse(radius_x, radius_y, 0, 100)$
e1: GiveRotatedEllipse(radius_x, radius_y, angle_step, 100)$
e2: GiveRotatedEllipse(radius_x, radius_y, 2*angle_step, 100)$


path:""$ 
 load(draw); 

 draw2d(
  user_preamble="set key top right; unset mouse",
  terminal  = 'png,
  file_name = sconcat(path,"e"),
 dimensions = [1000, 1000],
 proportional_axes = xy,
  line_width = 2,
  line_type = solid,
 fill_color = white,
  point_type=filled_circle,
  point_size = 0.5,
 key = "e0",
  color = red,
  e0,
  
  key= "e1",
  color=blue,
  e1,
  
  key= "e2",
  color= green,
  e2
 )$
  

and the result: enter image description here

All ellipses are rotated by the same angle. Where I did a mistake?

TIA

Adam

Adam
  • 1,254
  • 12
  • 25
  • 1
    Not sure where the problem is in that program, but it might be easier to get to a working solution by separating it into parts. First construct a rotation matrix, then construct a list of points, then apply the rotation to each point, then plot the points. When you have a rotation matrix `R` and a list of points `x`, you can say just: `x_rotated: map (lambda ([x1], R . x1), x)`. Try it first with a very short list of points so that you look at each one and see if it is right. Hope this helps. – Robert Dodier Dec 13 '20 at 18:28
  • @Robert Dodier : Thx for the tip. I did it here https://en.wikipedia.org/wiki/User:Adam_majewski/sandbox#ellipse also The code from R program seems similar, But I can check it. – Adam Dec 13 '20 at 18:37
  • 1
    Your "y: a*cos(t)*c + b*sin(t)*c" does not seem to be correct. Try changing it to "y: a*cos(t)*s + b*sin(t)*c" to see if it works. – fang Dec 13 '20 at 21:35
  • @fang convert comment to the answer – Adam Dec 14 '20 at 14:40
  • The result : https://commons.wikimedia.org/wiki/File:Nested_Ellipses.png – Adam Dec 16 '20 at 16:17
  • https://math.stackexchange.com/questions/1095827/scaling-and-rotating-a-square-so-that-it-is-inscribed-in-the-original-square – Adam Dec 16 '20 at 18:50

2 Answers2

1

The formula "y: acos(t)*c + bsin(t)*c" does not seem to be correct. Try changing it to "y: acos(t)*s + bsin(t)*c" to see if it works.

fang
  • 3,473
  • 1
  • 13
  • 19
0

The result, with help of fang is :

enter image description here

Adam
  • 1,254
  • 12
  • 25