• edited by
1,106 views
1 1 vote

Consider the line $y=\frac{n}{m}x$, where $n$ and $m$ are positive integers.

  1. If mq - np < 0, then is the point (p,q) above the line, below the line, or on the line?
  2. Complete the following function, that returns true if the line segment with endpoints (p,q) and (r,s) intersects the line $y=\frac{n}{m}x$, by writing the line number and the content of each box in your answer book.
  1: function clash (m, n, p, q, r, s: integer): Boolean;
  2: begin
  3: clash = false;
  4: if(m*q - n*p)⊏⊐ 0 then clash:= true;
  5: if(m*s - n*r)⊏⊐ 0 then clash:= true;
  6: if(m*q - n*p)⊏⊐ 0 and (m*s - n*r)⊏⊐ 0 then clash:= true;
  7: if(m*q - n*p)⊏⊐ 0 and (m*s - n*r)⊏⊐ 0 then clash:= true;
  8: end;

1 Answer

0 0 votes

(a) Above/Below Test

 

For the line \( y = \tfrac{n}{m}x \), define  

F(x,y) = m y - n x .

 

- If \(F(p,q) < 0\): the point \((p,q)\) is below the line.  

- If \(F(p,q) > 0\): the point \((p,q)\) is above the line.  

- If \(F(p,q) = 0\): the point \((p,q)\) lies on the line.  

 

So, if \(m q - n p < 0\), then \((p,q)\) is below the line.

 

(b) Completing the Function

 

Let  

A = F(p,q) = m q - n p, 

B = F(r,s) = m s - n r .

 

A line segment with endpoints \((p,q)\) and \((r,s)\) intersects the line if:

 

1. At least one endpoint lies on the line (\(A = 0\) or \(B = 0\)), OR  

2. The endpoints are on opposite sides (\(A > 0, B < 0\) or \(A < 0, B > 0\)).

 

Final Function

1: function clash (m, n, p, q, r, s: integer): Boolean;

2: begin

3: clash := false;

4: if (m*q - n*p) = 0 then clash := true; // P lies on the line

5: if (m*s - n*r) = 0 then clash := true; // R lies on the line

6: if (m*q - n*p) > 0 and (m*s - n*r) < 0 then clash := true; // Opposite sides

7: if (m*q - n*p) < 0 and (m*s - n*r) > 0 then clash := true; // Opposite sides

8: end;

Position:
Show:

Related questions

51 51 votes
8 answers 8 answers
17.5k
17.5k views
Kathleen asked Sep 14, 2014
17,547 views
A multiset is an unordered collection of elements where elements may repeat any number of times. The size of a multiset is the number of elements in it, counting repetiti...
13 13 votes
2 2 answers
4.4k
4.4k views
Kathleen asked Sep 14, 2014
4,384 views
Consider a bank database with only one relation transaction (transno, acctno, date, amount)The amount attribute value is positive for deposits and negative for withdrawa...
20 20 votes
2 2 answers
7.3k
7.3k views
Kathleen asked Sep 14, 2014
7,272 views
(a) Suppose you are given an empty $B^+$ tree where each node (leaf and internal) can store up to $5$ key values. Suppose values $1, 2,\ldots 10$ are inserted, in order, ...
46 46 votes
5 answers 5 answers
15.7k
15.7k views
Kathleen asked Sep 14, 2014
15,739 views
a. Fill in the boxes below to get a solution for the reader-writer problem, using a single binary semaphore, mutex (initialized to $1$) and busy waiting. Write the box nu...