javascript - Calculate map polygon Width and Height -
let's have 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 calculate maximum height , width of each polygon?
here 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 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 = 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 stored min , max distances between points, can check result in console here: http://jsfiddle.net/aaa_borza/eer8k/18/.
best regards!
Comments
Post a Comment