# 5.2. 点与矩形

## 5.2. 点与矩形

#### 5.2.1. 点到矩形的矩离

1.    计算$$\Delta =P-C$$;
2.    计算$${{s}_{0}}=\Delta \cdot {{\vec{e}}_{0}}$$和$${{s}_{1}}=\Delta \cdot {{\vec{e}}_{1}}$$;
3.    distSquare = 0;
4.    offset = s0 + u0/2;
5.    if offset < 0, then
6.           distSquare += offset*offset;
7.    else
8.           offset = s0 – u0/2;
9.           if offset > 0, then
10.                 distSquare += offset*offset;
11.          end if;
12.   end if;
13.   offset = s1 + u1/2;
14.   if offset < 0, then
15.          distSquare += offset*offset;
16.   else
17.          offset = s1 – u1/2;
18.          if offset > 0, then
19.                 distSquare += offset * offset;
20.          end if;
21.   end if;
22.   return distSquare;

#### 5.2.2. 点与矩形的关系

$\left\{ \begin{array}{l}{\Delta _1} = \left[ {(P – {V_0}) \times ({V_1} – {V_0})} \right] \cdot \left[ {(P – {V_2}) \times ({V_3} – {V_2})} \right]\\{\Delta _2} = \left[ {(P – {V_0}) \times ({V_2} – {V_0})} \right] \cdot \left[ {(P – {V_1}) \times ({V_3} – {V_1})} \right]\end{array} \right. \tag{5.2}$

1.    $${{V}_{0}}=C-{{u}_{0}}\cdot {{\vec{e}}_{0}}-{{u}_{1}}\cdot {{\vec{e}}_{1}}$$;
2.    $${{V}_{1}}=C+{{u}_{0}}\cdot {{\vec{e}}_{0}}-{{u}_{1}}\cdot {{\vec{e}}_{1}}$$;
3.    $${{V}_{2}}=C-{{u}_{0}}\cdot {{\vec{e}}_{0}}+{{u}_{1}}\cdot {{\vec{e}}_{1}}$$;
4.    $${{V}_{3}}=C+{{u}_{0}}\cdot {{\vec{e}}_{0}}+{{u}_{1}}\cdot {{\vec{e}}_{1}}$$;
5.    $${{\Delta }_{1}}=\left[ (P-{{V}_{0}})\times ({{V}_{1}}-{{V}_{0}}) \right]\cdot \left[ (P-{{V}_{2}})\times ({{V}_{3}}-{{V}_{2}}) \right]$$;
6.    if $${{\Delta }_{1}}\succ 0$$, then
7.           点P在矩形外;
8.    end if;
9.    $${{\Delta }_{2}}=\left[ (P-{{V}_{0}})\times ({{V}_{2}}-{{V}_{0}}) \right]\cdot \left[ (P-{{V}_{1}})\times ({{V}_{3}}-{{V}_{1}}) \right]$$;
10.   if $${{\Delta }_{2}}\succ 0$$, then
11.          点P在矩形外;
12.   end if;
13.   if $${{\Delta }_{1}}==0$$ || $${{\Delta }_{2}}==0$$, then
14.          点P在矩形的边上;
15.   end if;
16.   点P在矩形内;