0

Let's say I have this map:

<map name="diffmap1" id="diffmap1" class="myMap">
<area shape="poly" coords="152,347,253,292,264,307,167,358" class="diff diff1">
<area shape="poly" coords="93,244,164,215,171,233,97,264" class="diff diff2">
</map>

and jquery:

$('.diff').click(function(e){
//code here
});

How can I calculate the maximum height and width of each polygon?

Niklas
  • 13,005
  • 23
  • 79
  • 119
Speedwheelftw
  • 393
  • 6
  • 19
  • possible duplicate of [calculate width & height of poly area on image map](http://stackoverflow.com/questions/15477138/calculate-width-height-of-poly-area-on-image-map) – Pete Apr 24 '14 at 08:07
  • that is for the position, I want to get the height and width of the polygon – Speedwheelftw Apr 24 '14 at 08:45
  • max - min = width or height. Just don't divide it by 2 – Pete Apr 24 '14 at 08:47

1 Answers1

1

Here is the HTML code:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<img src="planets.gif" width="145" height="126" alt="Planets" usemap="#diffmap1">
<map name="diffmap1" id="diffmap1" class="myMap">
<area id="area1" shape="poly" coords="152,347,253,292,264,307,167,358" class="diff diff1">
<area id="area2" shape="poly" coords="93,244,164,215,171,233,97,264" class="diff diff2">
</map>

and the JS code:

function getDistance(p1,p2){
    return Math.sqrt((p2.y - p1.y)*(p2.y - p1.y) + (p2.x - p1.x)*(p2.x - p1.x));
}
$(document).ready(function(){
    var area_distances_min_max = new Object();
    $.each($('area'), function(){
        var arr_coords = $(this).attr('coords').split(',');
        var i = 0;
        var k = 0;
        var arr_real_coords = new Array();
        while(i<arr_coords.length){
            var obj = new Object();
            obj.x = arr_coords[i];
            obj.y = arr_coords[++i];
            arr_real_coords.push(obj);
            i++;
            k++;
        }

        var arr_distances = new Array();
        $.each(arr_real_coords, function(){
            var current_coord = this;
            $.each(arr_real_coords, function(){
                if(getDistance(current_coord,this)>0)arr_distances.push(getDistance(current_coord,this))
            })
        })

        var obj = new Object();

        obj.max = Math.max.apply(Math,arr_distances); 
        obj.min = Math.min.apply(Math,arr_distances);


        var id = $(this).attr('id')
        area_distances_min_max[id] = obj;
    })
    console.log(area_distances_min_max);
})

In array area_distances_min_max will be stored min and max distances between points, also you can check the result in console here: http://jsfiddle.net/aaa_borza/eer8k/18/.

Best regards!

Borza Adrian
  • 509
  • 2
  • 12
  • Here is the console result: area1 Object { max=118.92854997854805, min=18.601075237738275} area2 Object { max=83.00602387778854, min=19.313207915827967} – Borza Adrian Apr 24 '14 at 09:07