[R] Connecting a midpoint of a segment at a 90 degree angle to the next nearest point of a neighboring segment

Leo Mada |eo@m@d@ @end|ng |rom @yon|c@eu
Mon Sep 1 20:32:54 CEST 2025


Dear David,

I am replying to your question with a little delay. I hope though that you can find the following hints useful.

If I understand the task correctly:
You want to draw an orthogonal line to another line and find out where this line intersects a 3rd line.

You can decompose your problem into 2 separate tasks:
1. "Draw" a line orthogonal to the first line; (a physical drawing is not needed)
2. Compute the intersection of this new line with the desired line;

Task 1 is equivalent to translating a point orthogonally to a given line. I think you want to do it with the midpoint.

Have a look at the functions shiftLine and shift.ortho on GitHub:
https://github.com/discoleo/BioShapes/blob/main/R/Graphic.Helper.R

You can shift the point for a given distance, e.g. 1 unit - and thus have a new line segment.

### Shift Point or Line
# Shifts a point orthogonal to a given line;
# d = distance to shift (translate);
# simplify = TRUE: if(length(d) == 1) return a simple list(x, y);
#    otherwise return a data.frame;
# Note: direction of shift is not normalized with respect to quadrant;
[...]

Note:
- the "scale" argument can be used to plot on a non-square coordinate system and let the 2 lines *look*"prthogonal" on this non-square grid;
- if you do not need such functionality, you can set scale = 1 and simplify the formulas;

2. Intersection of segments
See function intersect.lines in the same file on GitHub.

The function returns the relative positions relative to the 2 line segments where they intersect, i.e.:
- Parameter t1 = Fraction of 1st line segment;
- Parameter t2 = Fraction of 2nd line segment;
Real intersection: 0 <= t <= 1;

You may also need the function slope() (in the same file). The functions in this file reference only other functions in this file (although I do not think there are any other calls).

Hope you can work it out.

Sincerely,

Leonard



	[[alternative HTML version deleted]]



More information about the R-help mailing list