groff
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Groff] Problems with arcs and angles


From: John Gardner
Subject: Re: [Groff] Problems with arcs and angles
Date: Fri, 28 Apr 2017 17:40:37 +1000

>
> Does this help or did I manage to misunderstand you completely?


I'm afraid so... the issue isn't with calculating radii, but calculating
the `startAngle` and `endAngle`. This is perhaps better explained with an
example:

https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/arc#frame_Playable_code

The following line draws a complete circle using the `arc` method:

ctx.arc(50, 50, 50, 0, 2 * Math.PI, false);


Where:
- Radius = 50px
- Start angle = 0
- End angle = 6.2832… radians (360 degrees)
- Arc centre = 50px, 50px

Of course, that's a deceptively simple-looking demo. Drawing partial curves
is proving more difficult.

In the demo's editor
<https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/arc#frame_Playable_code>,
press *Edit *and run this code instead:

let x = 100;
> let y = 100;
> let radius = 50;
> let startAngle = degToRad(45);
> let endAngle = degToRad(180);



ctx.beginPath();
> ctx.arc(x, y, radius, startAngle, endAngle);
> ctx.stroke();
> function radToDeg(value){
> return value * 180 / Math.PI;
> }
> function degToRad(value){
> return value * Math.PI / 180;
> }


If you tinker with the angles passed to `startAngle` and `endAngle`, you'll
see how the curvature is calculated.

Honestly, I'm starting to think this implementation is really backwards.
Kernighan's approach is much easier to get my head around...


On 28 April 2017 at 16:22, G. Branden Robinson <address@hidden
> wrote:

> At 2017-04-28T15:31:30+1000, John Gardner wrote:
> > This might be the wrong forum to ask for help with trigonometry, but I
> > was planning to announce progress eventually anyway.
>
> I have no experience with pic, but I faintly recall some trig.  Maybe I
> can help.
>
> > Now, this pic code:
> >
> > .PS
> > > "+" at 0,0
> > > arc -> from 0.5,0 to 0,0.5
> > > arc -> cw from 0,0 to 1,0.5
> > > arc -> cw from 0,0 to 2,0 rad 15
> > > .PE
> >
> >
> > ... should look like this
> > <https://cloud.githubusercontent.com/assets/2346707/25515071/4bc46870-
> 2c25-11e7-9883-9248e4b4aa68.png>.
> > Instead, it, uh, looks like this
> > <https://cloud.githubusercontent.com/assets/2346707/25515084/5d84eed6-
> 2c25-11e7-8cdd-bdb65f7804e9.png>
> > .
> >
> > Groff's output gives me these coordinates to go by:
> >
> >    - startX, startY - Coordinates of the arc's starting point
> >    - centreX, centreY - Coordinates of the arc's centre
> >    - endX, endY - Coordinates of the arc's terminal point
> >
> > But the canvas arc method I'm working with requires all of these:
> >
> >    - x - The x coordinate of the arc's centre.
> >    - y - The y coordinate of the arc's centre.
> >    - radius - The arc's radius.
> >    - startAngle - The angle at which the arc starts, measured clockwise
> >    from the positive x axis and expressed in radians.
> >    - endAngle - The angle at which the arc ends, measured clockwise from
> >    the positive x axis and expressed in radians.
> >
> > It's embarrassing to be stuck on something so obvious, but this is what's
> > blocking further progress...
>
> According to pic.ms, the default radius for an arc is 0.5 inches.  I
> mention this because to convert between angles and arc endpoint
> coordinates you need to know the radius.
>
> The coordinates of the endpoint of the terminal side of an angle are
> expressed by this relation:
>
> Let θ be an angle in radians between the initial (i) and final (f) radii
> connecting the endpoints of an arc to their common center.
>
> Then x and y, the cartesian coordiates of the arc's endpoint are given
> by
>
> x = r cos θ
>
> and
>
> y = r sin θ
>
> where r is the radius of the arc.
>
> It sounds like you need the inverse relations, beause you have (x_i,
> y_i) [startX and startY] and (x_f, y_f) [endX and endY] but you want θ_i
> and θ_f for the canvas object.  Solving for θ we find:
>
> θ = arccos(x/r) = arcsin(y/r)
>
> The trig functions are periodic, not one-to-one, so you will need to
> keep track of the signs on x and y so that the θ you find is in the
> correct quadrant.
>
> Be prepared to add or subtract multiples of π/2 to the θ you find to
> account for the quadrant.  My guess is that Kernighan chose the approach
> he did when designing pic (contra that of the HTML canvas folks) because
> it preserves more information.
>
> Does this help or did I manage to misunderstand you completely?
>
> Regards,
> Branden
>


reply via email to

[Prev in Thread] Current Thread [Next in Thread]