日期:2014-05-16  浏览次数:20388 次

zcy_dr高手.请帮下忙.
HTML code

<img src="images/1111.jpg" width="500" height="300"  usemap="#map_sample" border="0" />
<map name="map_sample" id="map_sample">
<area shape="poly" coords="284,47,270,88" href="#" />
<area shape="poly" coords="340,164,304,123" href="#" />
</map>
<script>
function foo(pos1,pos2,pos3,pos4){
var x,y;
    if(pos1.x==pos2.x && pos1.y != pos2.y){
        if(pos3.x==pos4.x && pos3.y != pos4.y)
            return false;
        x = pos1.x;
        var k2 = (pos3.y-pos4.y)/(pos3.x-pos4.x);
        var b2 = pos3.y - k2*pos3.x;
        y = k2*x+b2;        
    }else if(pos3.x==pos4.x && pos3.y != pos4.y){
        x = pos3.x;
        var k1 = (pos1.y-pos2.y)/(pos1.x-pos2.x);
        var b1 = pos1.y - k1*pos1.x;
        y = k1*x+b1;
    }else{    
        var k1 = (pos1.y-pos2.y)/(pos1.x-pos2.x);
        var k2 = (pos3.y-pos4.y)/(pos3.x-pos4.x);
        
        if(k1 == k2){
            return false;
        }
        
        var b1 = pos1.y - k1*pos1.x;
        var b2 = pos3.y - k2*pos3.x;
        
        x = (b2-b1)/(k1-k2);
        y = k1*x+b1;    
    }
    
    if(((x<=pos1.x && x>=pos2.x) || (x<=pos2.x && x>=pos1.x)) && ((x<=pos3.x && x>=pos4.x) || (x<=pos4.x && x>=pos3.x)))
        return {x:x,y:y};
    else
        return false;

}
//326,123,354,84
var r = foo({x:284,y:47},{x:270,y:88},{x:340,y:164},{x:304,y:123});

if(r)
    alert("交点为:x:"+r.x+",y:"+r.y);
else
    alert("没有交点");
</script>


可否将你这个再判断下。
当一条钱段的端点在另一条线段上。
也让他提示相交。并返回该端点。

------解决方案--------------------
交点的x坐标为277688/1025,约等于270.9,不是任何一个端点