Posted on 2009-08-13 17:21
Hero 阅读(721)
评论(0) 编辑 收藏 引用
今天用C#写了google map api 编码折线算法,C#用的很不习惯,感觉还是C++的好用,中文的翻译让我很难读懂,如果有错误,请指正
http://code.google.com/intl/zh-CN/apis/maps/documentation/polylinealgorithm.html
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4
5 namespace ConsoleApplication1
6 {
7 class Program
8 {
9 static string trans( double source )
10 {
11 //获得绝对值
12 int absSource ;
13 if ( source >= 0 )
14 {
15 absSource = (int)( source * 100000 + 0.5 );
16 }
17 else
18 {
19 absSource = (int)( source * 100000 - 0.5 ) ;
20 }
21 absSource = Math.Abs( absSource );
22 //Console.WriteLine( absSource );
23
24 //获得二进制
25 StringBuilder binstring1 = new StringBuilder();
26 binstring1.Append( Convert.ToString( absSource, 2 ) );
27 //Console.WriteLine( binstring1 );
28
29 StringBuilder binstring2 = new StringBuilder();
30 for ( int i = 0; i < 32-binstring1.Length; i++ )
31 {
32 binstring2.Append( "0" );
33 }
34 binstring2.Append( binstring1 ) ;
35 //Console.WriteLine( binstring2 ) ;
36
37 //如果是负数 -- 取反加1
38 if ( source < 0 )
39 {
40 //Console.WriteLine( "负数 -- 取反加一" );
41 for ( int i = 0; i < binstring2.Length; i++ )
42 {
43 if ( '0' == binstring2[i] ) binstring2[i] = '1';
44 else binstring2[i] = '0';
45 }
46 //Console.WriteLine( binstring2 ) ;
47 int carry = 1;
48 //binstring2[binstring2.Length - 1]++;
49 for ( int i = binstring2.Length - 1 ; i >= 0; i-- )
50 {
51 while( carry > 0 )
52 {
53 carry = carry - 1 ;
54 binstring2[i] ++ ;
55 }
56
57 if ( '2' == binstring2[i] )
58 {
59 carry = 1;
60 binstring2[i] = '0';
61 }
62 else
63 {
64 carry = 0;
65 }
66 }
67 if( carry > 0 )
68 {
69 binstring2.Insert( 0, '1' ) ;
70 }
71 //Console.WriteLine( binstring2 ) ;
72 }//取反加一
73
74 //变换二进制值
75 binstring2.Append( "0" ) ;
76 //Console.WriteLine( binstring2 ) ;
77
78 //如果原来是负数 -- 对编码求反
79 if( source < 0 )
80 {
81 for( int i=0; i<binstring2.Length; i++ )
82 {
83 if( '0' == binstring2[i] ) binstring2[i] = '1' ;
84 else binstring2[i] = '0' ;
85 }
86
87 //Console.WriteLine( "负数 -- 对编码求反" ) ;
88 //Console.WriteLine( binstring2 ) ;
89 }
90
91 //从右侧开始五五分组
92 int[] num = new int[10] ; int cnum = 0 ;
93 string[] strnum = new string[10] ;
94
95 string binstr = binstring2.ToString();
96 for( int i=binstring2.Length, count=0; i>=0; i--, count++ )
97 {
98 if( 5 == count || 0 == i )
99 {
100 //strnum[cnum++] = binstring1.ToString() ;
101 binstring1.Remove( 0, binstring1.Length ) ;
102 binstring1.Append( binstr, i, count );
103 strnum[cnum++] = binstring1.ToString();
104 //Console.WriteLine( strnum[cnum-1] ) ;
105 count = 0;
106 }
107 }
108
109 //去除最后的 0 块
110 for( int i=cnum-1; i>0; i-- )
111 {
112 bool del = true ;
113 foreach ( char var in strnum[i] )
114 {
115 if( var != '0' ) del = false ;
116 }
117
118 if( del ) cnum-- ;
119 else break ;
120 }
121 //Console.WriteLine( cnum ) ;
122
123
124 //与0x20或
125 for( int i=0; i<cnum; i++ )
126 {
127 if( i != cnum - 1 )
128 {
129 num[i] = Convert.ToInt32( strnum[i], 2 ) ;
130 num[i] = num[i] | 0x20 ;
131 }
132 else
133 {
134 num[i] = Convert.ToInt32( strnum[i], 2 ) ;
135 }
136
137 num[i] += 63 ;
138 //Console.WriteLine( num[i] ) ;
139 }
140
141 StringBuilder restring = new StringBuilder() ;
142 for ( int i = 0; i < cnum; i++ )
143 {
144 restring.Append( Convert.ToChar( num[i] ) );
145 }
146
147 return restring.ToString();
148 }
149
150
151 static void Main( string[] args )
152 {
153 //string input = null ;
154 //while ( (input = Console.ReadLine().Trim().ToUpper() ) != "Q" )
155 //{
156 // double x = Convert.ToDouble( input.Trim() );
157 // Console.WriteLine( trans( x ) );
158 //}
159
160 Console.WriteLine( trans(38.5) );
161 Console.WriteLine( trans(-120.2) );
162 Console.WriteLine( trans( 40.7-38.5 ) );
163 Console.WriteLine( trans( -120.95 - (-120.2) ) );
164 Console.WriteLine( trans( 43.252-40.7 ) );
165 Console.WriteLine( trans( -126.453-(-120.95) ) );
166 }
167 }
168 }