Sharing Inherited Land
C

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;memory.h&gt;



enum mode
{
    CORD_MODE_CONST = 1,
    CORD_MODE_SLOPE = 2,
};
typedef struct point {
    float x;
    float y;
} CORD;

typedef struct l {
    int mode;
    float m;
    float b;
}LINE;  

typedef struct r {

    CORD topL;
    CORD botL;

    CORD topR;
    CORD botR;

    CORD midRight;
    CORD midBot;
    CORD midLeft;
    CORD midTop;

    CORD center;
    LINE vertLine;
    LINE horLine;

} RECTANGLE;


void findBorderIntersection(RECTANGLE * pR, LINE * p);
int isPointContained(RECTANGLE * pR, CORD * pC1);
float dotProduct(float y1, float y2, float x1, float x2);
void findCenterOfRetangle(RECTANGLE * pR);
void findIntersection(LINE * pL1, LINE * pL2, CORD *pInt);
void findMidPointOfLine(CORD * pC1, CORD * pC2, CORD * pMid);
void getLine(CORD * pC1, CORD * pC2, LINE * pLine );

void dumpCord(char * pS, CORD * pC)
{
    printf(" %s y:%f x:%f\n",pS,pC-&gt;y,pC-&gt;x);
}

int main()
{
    RECTANGLE yourLand;
    RECTANGLE cityLand;
    LINE  lineThroughCenter;

    memset(&amp;yourLand,0x0, sizeof(RECTANGLE));
    memset(&amp;cityLand,0x0, sizeof(RECTANGLE));

    scanf("%f%f%f%f%f%f%f%f",&amp;(yourLand.topL.x),
                             &amp;(yourLand.topL.y),
                             &amp;(yourLand.topR.x),
                             &amp;(yourLand.topR.y),
                             &amp;(yourLand.botL.x),
                             &amp;(yourLand.botL.y),
                             &amp;(yourLand.botR.x),
                             &amp;(yourLand.botR.y));

    scanf("%f%f%f%f%f%f%f%f",&amp;(cityLand.topL.x),
                             &amp;(cityLand.topL.y),
                             &amp;(cityLand.topR.x),
                             &amp;(cityLand.topR.y),
                             &amp;(cityLand.botL.x),
                             &amp;(cityLand.botL.y),
                             &amp;(cityLand.botR.x),
                             &amp;(cityLand.botR.y));

    /* Find center of your land */
    findCenterOfRetangle(&amp;yourLand);

    /* Find center of city land */
    findCenterOfRetangle(&amp;cityLand);

    /* Handle the case where the centers are right on top of each other */
    if((yourLand.center.x == cityLand.center.x) &amp;&amp;
     (yourLand.center.y == cityLand.center.y))
    {
        /* Find the line through center and the top border */
        getLine(&amp;(yourLand.midTop),&amp;(cityLand.center),&amp;lineThroughCenter);
    }
    else
    {
        /* Find the line through center of both lands */
        getLine(&amp;(yourLand.center),&amp;(cityLand.center),&amp;lineThroughCenter);
    }

    /* Find the border cords */
    findBorderIntersection(&amp;(yourLand),&amp;lineThroughCenter);

    return 0;
}
void findBorderIntersection(RECTANGLE * pR, LINE * p)
{
    LINE line;
    CORD c1;
    CORD c2;

    int bTop = 0;

    getLine(&amp;(pR-&gt;topL), &amp;(pR-&gt;topR),&amp;line);

    /* Figure out if the the line goes through the top/bottom or right/left */
    if( p-&gt;m != line.m)
    {
          findIntersection(p,&amp;line, &amp;c1);
          bTop = isPointContained(pR,&amp;c1);
    }

    if(bTop)
    {
        getLine(&amp;(pR-&gt;botL), &amp;(pR-&gt;botR),&amp;line);
        findIntersection(p,&amp;line, &amp;c2);
    }
    else
    {
        getLine(&amp;(pR-&gt;topL), &amp;(pR-&gt;botL),&amp;line);
        findIntersection(p,&amp;line, &amp;c1);

        getLine(&amp;(pR-&gt;topR), &amp;(pR-&gt;botR),&amp;line);
        findIntersection(p,&amp;line, &amp;c2);
    }

    dumpCord("Border Cord1:",&amp;c1);
    dumpCord("Border Cord2:",&amp;c2);

}

int isPointContained(RECTANGLE * pR, CORD * pC1)
{
    float pointProduct1;
    float vectorProduct1;
    float pointProduct2;
    float vectorProduct2;

    vectorProduct1 = dotProduct((pR-&gt;topL.y - pR-&gt;botL.y), 
                               (pR-&gt;topL.y - pR-&gt;botL.y),
                               (pR-&gt;topL.x - pR-&gt;botL.x),
                               (pR-&gt;topL.x - pR-&gt;botL.x));

    pointProduct1 = dotProduct((pC1-&gt;y - pR-&gt;botL.y),
                              (pR-&gt;topL.y - pR-&gt;botL.y),
                              (pC1-&gt;x - pR-&gt;botL.x),
                              (pR-&gt;topL.x - pR-&gt;botL.x));

    vectorProduct2 = dotProduct((pR-&gt;botR.y - pR-&gt;botL.y), 
                               (pR-&gt;botR.y - pR-&gt;botL.y),
                               (pR-&gt;botR.x - pR-&gt;botL.x),
                               (pR-&gt;botR.x - pR-&gt;botL.x));

    pointProduct2 = dotProduct((pC1-&gt;y - pR-&gt;botL.y),
                              (pR-&gt;botR.y - pR-&gt;botL.y),
                              (pC1-&gt;x - pR-&gt;botL.x),
                              (pR-&gt;botR.x - pR-&gt;botL.x));


    if( (pointProduct1 &lt;= vectorProduct1) &amp;&amp;
        (pointProduct1 &gt;= 0) &amp;&amp; 
        (pointProduct2 &lt;= vectorProduct2) &amp;&amp;
        (pointProduct2 &gt;= 0))
    {    
        return 1;
    }

    return 0;

}
float dotProduct(float y1, float y2, float x1, float x2)
{
    return y1*y2 + x1*x2;
}

void findCenterOfRetangle(RECTANGLE * pR)
{
     /* Find the mid point of the left side */
    findMidPointOfLine(&amp;(pR-&gt;topL), &amp;(pR-&gt;botL), &amp;(pR-&gt;midLeft));

     /* Find the mid point of the right side */
    findMidPointOfLine(&amp;(pR-&gt;topR), &amp;(pR-&gt;botR), &amp;(pR-&gt;midRight));

    /* Get the line */    
    getLine(&amp;(pR-&gt;midLeft), &amp;(pR-&gt;midRight), &amp;(pR-&gt;horLine));

     /* Find the mid point of the top side */
    findMidPointOfLine(&amp;(pR-&gt;topL), &amp;(pR-&gt;topR), &amp;(pR-&gt;midTop));

     /* Find the mid point of the bottom side */
    findMidPointOfLine(&amp;(pR-&gt;botL), &amp;(pR-&gt;botR), &amp;(pR-&gt;midBot));

    /* Get the line */    
    getLine(&amp;(pR-&gt;midTop), &amp;(pR-&gt;midBot), &amp;(pR-&gt;vertLine));

    /* Find the center of the first rectangle */
    findIntersection(&amp;(pR-&gt;horLine), &amp;(pR-&gt;vertLine),&amp;(pR-&gt;center));


}
void findIntersection(LINE * pL1, LINE * pL2, CORD *pInt)
{

    if(pL1-&gt;mode == CORD_MODE_CONST)
    {
        pInt-&gt;x = pL1-&gt;m;
        pInt-&gt;y = ((pL2-&gt;m * pInt-&gt;x) + pL2-&gt;b);
    }
    else if(pL2-&gt;mode == CORD_MODE_CONST)
    {
        pInt-&gt;x = pL2-&gt;m;
        pInt-&gt;y = ((pL1-&gt;m * pInt-&gt;x) + pL1-&gt;b);
    }
    else
    {
        pInt-&gt;x = (pL2-&gt;b - pL1-&gt;b) / (pL1-&gt;m - pL2-&gt;m);
        pInt-&gt;y = ((pL2-&gt;m * pInt-&gt;x) + pL2-&gt;b);
    }
}

void findMidPointOfLine(CORD * pC1, CORD * pC2, CORD * pMid)
{
    pMid-&gt;y = (pC1-&gt;y + pC2-&gt;y) / 2;
    pMid-&gt;x = (pC2-&gt;x + pC1-&gt;x) / 2;
}
void getLine(CORD * pC1, CORD * pC2, LINE * pLine )
{

    /* Find slope */
    if((pC1-&gt;x - pC2-&gt;x) == 0)
    {
        pLine-&gt;mode = CORD_MODE_CONST;
        pLine-&gt;m = pC1-&gt;x;
        pLine-&gt;b = 0;
    }
    else
    {
        pLine-&gt;mode = CORD_MODE_SLOPE;
        pLine-&gt;m = ((pC1-&gt;y - pC2-&gt;y) / (pC1-&gt;x - pC2-&gt;x));

        /* Find y inter. */
        pLine-&gt;b = pC1-&gt;y - (pLine-&gt;m * pC1-&gt;x);
    }
}