root / trunk / web / dojo / dojox / drawing / util / common.js @ 12
History | View | Annotate | Download (2.88 KB)
1 | 9 | andrej.cim | /*
|
---|---|---|---|
2 | Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
|
||
3 | Available via Academic Free License >= 2.1 OR the modified BSD license.
|
||
4 | see: http://dojotoolkit.org/license for details
|
||
5 | */
|
||
6 | |||
7 | |||
8 | if(!dojo._hasResource["dojox.drawing.util.common"]){ |
||
9 | dojo._hasResource["dojox.drawing.util.common"]=true; |
||
10 | dojo.provide("dojox.drawing.util.common");
|
||
11 | dojo.require("dojox.math.round");
|
||
12 | (function(){
|
||
13 | var _1={};
|
||
14 | var _2=0; |
||
15 | dojox.drawing.util.common={radToDeg:function(n){ |
||
16 | return (n*180)/Math.PI; |
||
17 | },degToRad:function(n){ |
||
18 | return (n*Math.PI)/180; |
||
19 | },angle:function(_3,_4){ |
||
20 | if(_4){
|
||
21 | _4=_4/180;
|
||
22 | var _5=this.radians(_3),_6=Math.PI*_4,_7=dojox.math.round(_5/_6),_8=_7*_6; |
||
23 | return dojox.math.round(this.radToDeg(_8)); |
||
24 | }else{
|
||
25 | return this.radToDeg(this.radians(_3)); |
||
26 | } |
||
27 | },oppAngle:function(_9){ |
||
28 | (_9+=180)>360?_9=_9-360:_9; |
||
29 | return _9;
|
||
30 | },radians:function(o){ |
||
31 | return Math.atan2(o.start.y-o.y,o.x-o.start.x);
|
||
32 | },length:function(o){ |
||
33 | return Math.sqrt(Math.pow(o.start.x-o.x,2)+Math.pow(o.start.y-o.y,2)); |
||
34 | },lineSub:function(x1,y1,x2,y2,_a){ |
||
35 | var _b=this.distance(this.argsToObj.apply(this,arguments)); |
||
36 | _b=_b<_a?_a:_b; |
||
37 | var pc=(_b-_a)/_b;
|
||
38 | var x=x1-(x1-x2)*pc;
|
||
39 | var y=y1-(y1-y2)*pc;
|
||
40 | return {x:x,y:y}; |
||
41 | },argsToObj:function(){ |
||
42 | var a=arguments; |
||
43 | if(a.length<4){ |
||
44 | return a[0]; |
||
45 | } |
||
46 | return {start:{x:a[0],y:a[1]},x:a[2],y:a[3]}; |
||
47 | },distance:function(){ |
||
48 | var o=this.argsToObj.apply(this,arguments); |
||
49 | return Math.abs(Math.sqrt(Math.pow(o.start.x-o.x,2)+Math.pow(o.start.y-o.y,2))); |
||
50 | },slope:function(p1,p2){ |
||
51 | if(!(p1.x-p2.x)){
|
||
52 | return 0; |
||
53 | } |
||
54 | return ((p1.y-p2.y)/(p1.x-p2.x));
|
||
55 | },pointOnCircle:function(cx,cy,_c,_d){ |
||
56 | var _e=_d*Math.PI/180; |
||
57 | var x=_c*Math.cos(_e);
|
||
58 | var y=_c*Math.sin(_e);
|
||
59 | return {x:cx+x,y:cy-y}; |
||
60 | },constrainAngle:function(_f,min,max){ |
||
61 | var _10=this.angle(_f); |
||
62 | if(_10>=min&&_10<=max){
|
||
63 | return _f;
|
||
64 | } |
||
65 | var _11=this.length(_f); |
||
66 | var _12=_10>max?max:min-_10<100?min:max; |
||
67 | return this.pointOnCircle(_f.start.x,_f.start.y,_11,_12); |
||
68 | },snapAngle:function(obj,ca){ |
||
69 | var _13=this.radians(obj),_14=this.length(obj),seg=Math.PI*ca,rnd=Math.round(_13/seg),_15=rnd*seg,_16=this.radToDeg(_15),pt=this.pointOnCircle(obj.start.x,obj.start.y,_14,_16); |
||
70 | return pt;
|
||
71 | },idSetStart:function(num){ |
||
72 | _2=num; |
||
73 | },uid:function(str){ |
||
74 | str=str||"shape";
|
||
75 | _1[str]=_1[str]===undefined?_2:_1[str]+1; |
||
76 | return str+_1[str];
|
||
77 | },abbr:function(_17){ |
||
78 | return _17.substring(_17.lastIndexOf(".")+1).charAt(0).toLowerCase()+_17.substring(_17.lastIndexOf(".")+2); |
||
79 | },mixin:function(o1,o2){ |
||
80 | },objects:{},register:function(obj){ |
||
81 | this.objects[obj.id]=obj;
|
||
82 | },byId:function(id){ |
||
83 | return this.objects[id]; |
||
84 | },attr:function(_18,_19,_1a,_1b){ |
||
85 | if(!_18){
|
||
86 | return false; |
||
87 | } |
||
88 | try{
|
||
89 | if(_18.shape&&_18.util){
|
||
90 | _18=_18.shape; |
||
91 | } |
||
92 | if(!_1a&&_19=="id"&&_18.target){ |
||
93 | var n=_18.target;
|
||
94 | while(!dojo.attr(n,"id")){ |
||
95 | n=n.parentNode; |
||
96 | } |
||
97 | return dojo.attr(n,"id"); |
||
98 | } |
||
99 | if(_18.rawNode||_18.target){
|
||
100 | var _1c=Array.prototype.slice.call(arguments); |
||
101 | _1c[0]=_18.rawNode||_18.target;
|
||
102 | return dojo.attr.apply(dojo,_1c);
|
||
103 | } |
||
104 | return dojo.attr(_18,"id"); |
||
105 | } |
||
106 | catch(e){
|
||
107 | if(!_1b){
|
||
108 | } |
||
109 | return false; |
||
110 | } |
||
111 | }}; |
||
112 | })(); |
||
113 | } |