I want to calculate efficiently which coordinates (x, y) lie between (x1, y1) and (x2, y2), as indicated in the same line. The inequalities (min and max) are therefore valid line by line and not the entire column of values (x1, y1) or (x2, y2). I would test (cond1) if both x> = min (x1, x2) and x <=max(x1, x2), and (cond2) if both y >= min (y1, y2) and y <= max (y1, y2).

```
x1 <- c (7, 8, 2, 2, 2, 7, 3)
y1 <- c (9, 3, 2, 5, 8, 7, 9)
x2 <- c (4, 2, 7, 9, 5, 7, 5)
y2 <- c (0, 6, 1, 8, 3, 5, 3)
x <- c (0, 7, 3, 9, 3, 6, 6)
y <- c (7, 0, 7, 4, 8, 9, 7)
K <- data frames (x1, y1, x2, y2, x, y)
```

The output would look like this:

```
x1 y1 x2 y2 x y cond1 cond2
1 7 9 4 0 0 7 0 1
2 8 3 2 6 7 0 1 0
3 2 2 7 1 3 7 1 0
4 2 5 9 8 9 4 1 0
5 2 8 5 3 3 8 1 1
6 7 7 7 5 6 9 0 0
7 3 9 5 3 6 7 0 1
```

Can this be done with Apply and a custom function? If executed as a loop, what is the most efficient way to reference the line in the inequalities?