Given a line of two points, constrict the drawing of the next line within a certain angle.

146 Views Asked by At

Presume I have a line made up of two points on a 2D plane. We'll call the line AB, and it is drawn randomly with a random length and angle to the global X axis. I then need to draw line BC, which spans from the end of line AB and adds a new point: 'C'. This BC line needs to be constricted to a certain angle. The actual angle doesn't matter.

Consider this crude diagram

The green area represents the ambiguous area in which the next point can be drawn. Note that the length of the line should also be variable (i.e. the distance of point C from B).

EDIT: To clear things up, the allowed angle area should be influenced by the angle of the AB line against the global X

See crude diagram 2.0

In short, the question is. How would I mathematically define an "allowed drawing space" for point C so it can be any variable length and be constrained within the green area. The green area should be influenced by the line AB, based on its angle to the global X.

Thanks!

2

There are 2 best solutions below

4
On BEST ANSWER

Taking into account your objective, i.e., build a rather smooth broken line without sudden orientation changes, here is an algorithm that performs the work:

$\text{Initialisation} \ : \ \theta_0=0, \ x_0=0, \ y_0=0, \ n=500, \ a=0.2$

$\text{Repeat for } \ k=1 \ \text{to} \ n:$

$\begin{array}{l} \ \ \ \ \ \ \text{AngleVariation = (2*random - 1)}*a*\pi \\ \ \ \ \ \ \ \theta_{k+1} = \theta_{k} + \text{AngleVariation} \\ \ \ \ \ \ \ r_{k} = \text{random} \\ \ \ \ \ \ \ x_{k+1} = x_{k} + r_{k}*\cos(\theta_{k}) \\ \ \ \ \ \ \ y_{k+1} = y_{k} + r_{k}*\sin(\theta_{k}) \\ \ \ \ \ \ \ \text{plot segment line from point} (x_{k}, y_{k}) \ \text{to point} \ (x_{k+1}, y_{k+1}) \end{array}$

Explanation :

  • $a$, set here to $0.2$, is an adjustable parameter controlling the aperture of the angle.

  • "random" is a uniformly distributed number with value between $0$ and $1$. Every time it's called, it is a new random number that is generated. In this way "2*random - 1" is uniformly distributed between $-1$ and $1$.

  • the choice of the number of line segments $n=500$ is, of course, arbitrary.

  • $\pi$ (in radians) is the same as $180°$ (in degrees).

Illustration :

I have programmed the algorithm with Matlab. Here are two results, the first one with $a=0.1$, the second one with $a=0.3$, showing the influence of this parameter.

enter image description here

enter image description here

5
On

Basically you will need to determine the equations of the green lines, for example $y = ax+b$ for the lower line and $y = cx+d$ for the upper line, where the slopes $a$ and $c$ can be found from the angle of line AB and the allowable angle between AB and BC and then the intercepts $b$ and $d$ would be found to satisfy each line so the intersect point B. Then the representation of the allowable point would be in the form $ax+b < y < cx+d$ or as the system $ax+b < y$, $y < cx+d$. Any point that satisfies both inequalities simultaneously would work.

EDIT: As pointed out in the comments this is only true if both lines go to the left or both lines go to the right. If one green line goes to the left and the other to the right or if either (or both) is vertical then my solution is invalid.