<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Tamir Khason - Just code &#187; Math</title>
	<atom:link href="http://khason.net/tag/math/feed/" rel="self" type="application/rss+xml" />
	<link>http://khason.net</link>
	<description>Take care of the sense, and the sounds will take care of themselves.</description>
	<lastBuildDate>Thu, 15 Apr 2010 13:25:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Math world, simple mental calculations or what&#8217;s going on with education?</title>
		<link>http://khason.net/blog/math-world-simple-mental-calculations-or-whats-going-on-with-education/</link>
		<comments>http://khason.net/blog/math-world-simple-mental-calculations-or-whats-going-on-with-education/#comments</comments>
		<pubDate>Sun, 08 Feb 2009 13:07:55 +0000</pubDate>
		<dc:creator>Tamir</dc:creator>
				<category><![CDATA[BLOG]]></category>
		<category><![CDATA[blogging general]]></category>
		<category><![CDATA[fun]]></category>
		<category><![CDATA[help]]></category>
		<category><![CDATA[jobs]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[quiz]]></category>
		<category><![CDATA[thoughts]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://khason.net/blog/math-world-simple-mental-calculations-or-whats-going-on-with-education/</guid>
		<description><![CDATA[Today, I want to write blog post which is absolutely not related to programming. It related to math and education in general those days. During work interviews, I see a lot of people, who was absolutely unable to calculate mentally. They just can’t understand, that it’s possible to do without calculators. When my kids (2nd, [...]

<p/>]]></description>
			<content:encoded><![CDATA[<p>Today, I want to write blog post which is absolutely not related to programming. It related to math and education in general those days. During <a href="http://khason.net/tech/brilliant-yet-simple-technical-questions-can-be-used-for-work-interview/" target="_blank">work interviews</a>, I see a lot of people, who was absolutely unable to calculate mentally. They just can’t understand, that it’s possible to do without calculators. When my kids (2nd, 6th and 7th grade) were small I taught them to play with numbers, and until 4th grade (bigger kids) they were able do it. but then school teachers “killed” this ability. Why people should use calculator for simple math operations, if he can do it mentally? Shame you, the modern educational system. Let’s go back and try to understand how people were able to live without devil devices, such as calculators…</p>
<p>Following the paint of <a href="http://en.wikipedia.org/wiki/Nikolai_Bogdanov-Belsky" target="_blank">Nikolai Bogdanov-Belsky</a> “Counting in their heads”. This painting is dated 1895.</p>
<p><img title="Богданов Бельский - Устный счёт" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="570" alt="Богданов Бельский - Устный счёт" src="http://khason.net/images/2009/02/image1.png" width="714" border="0" /> </p>
<p>As you can see at the painting, peasant kids trying to solve following exercise mentally:</p>
<blockquote><p>(10<sup>2</sup> + 11<sup>2</sup> + 12<sup>2</sup> + 13<sup>2</sup> + 14<sup>2</sup>) / 365</p>
</blockquote>
<p>This is not very simple exercise, especially when should be solved without your favorite calculator. However, when I was 4th grade I learned to square two-digit numbers mentally (my, and I think, yours too): <strong>First, find the nearest multiple of ten, by raising or lowering your number, then add and remove the rest to each of numbers and add the square of oddment</strong>. For example</p>
<blockquote><p>45 * 45 = (45+5) * (45-5) + (5 * 5) = 50 * 40 + 25 = (5 * 4) * 100 + 25 = 20 * 100 + 25 = 2000 + 25 = 2025      <br />14 * 14 = (14+4) * (14-4) + (4 * 4) = 18 * 10 + 16 = 180 + 16 = 196</p>
</blockquote>
<p>So, now it can be solved easily:</p>
<blockquote><p>10<sup>2</sup> = 100       <br />11<sup>2</sup>= (11+1) * (11-1) + 1 = 12 * 10 + 1 = 121      <br />12<sup>2</sup>= (12+2) * (12-2) + 4 = 14 * 10 + 4 = 144      <br />13<sup>2</sup>= (13+3) * (13-3) + 9 = 16 * 10 + 9 = 169      <br />14<sup>2</sup>= (14+4) * (14-4) + 16 = 18 * 10 + 16 = 196</p>
</blockquote>
<p>And so on… but wait, 100+121+144 already equals 365, which is our denominator. Next sequence will bring us 169+196, which is also 365. So the answer to this black board brain teaser is 2.</p>
<p>However, it can be rather complicated to calculate 86<sup>2</sup> for instance:</p>
<blockquote><p>86<sup>2</sup> = (86 + 4) * (86 – 4) + (4 * 4) = 90 * 82 + 16…</p>
</blockquote>
<p>Let’s try another way – <strong>multiple the difference between the number and 25 by 100, then add the square of the difference or excess of the number and 50</strong>. For example</p>
<blockquote><p>86<sup>2</sup> = (86 – 25) * 100 + (86 – 50)<sup>2</sup> = 61 * 100 +&#160; 36<sup>2</sup> = 6100 + (36 – 25) * 100 + (50 &#8211; 36)<sup>2</sup> = 6100 + 1100 + 14<sup>2</sup> = 7200 + 196 = 7396</p>
</blockquote>
<p>Isn’t it really simple and fun to calculate squares of numbers?</p>
<p><em>Bonus</em>: how to calculate multiple of two digit numbers with the sum of its unity digits equals to 10? </p>
<ol>
<li>Multiply first digit of the first number by 10</li>
<li>Add 1 to first digit of second number and multiply the result by 10</li>
<li>Multiply results of step 1 and step 2</li>
<li>Deduct second number and the result of step 1</li>
<li>Multiply second digit of the first number by the result of step 4</li>
<li>Add results of steps 3 and 5</li>
</ol>
<p>Looks complicated? Let’s make it easier. Assuming that first number is X = 10x + z and second number is Y = 10y + (10 – z), the formula for quick multiplication calculation is: <strong>100 * x * (y + 1) + z * (Y – 10 * x)</strong>. For example:</p>
<blockquote><p>96 * 84 = 100 * 9 * (8+1) + 6 * (84-10 * 9) = 100 * 9 * 9 + 6 * (84 – 90) = 8100 – 6 * 6 = 8100 – 36 = 8064     <br />37 * 93 = 100 * 3 * (9+1) + 7 * (93 – 10 * 3) = 3000 + 7 * 63 = 3000 + (100 * 6 * 1 + 3 * (7 – 60)) = 3000 + 600 – 3 * 53 = 3600 – 159 = 3441</p>
</blockquote>
<p>Have a nice day and be good people. Also, throw out all hardware calculators and uninstall all software <img src='http://khason.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>


<p/>]]></content:encoded>
			<wfw:commentRss>http://khason.net/blog/math-world-simple-mental-calculations-or-whats-going-on-with-education/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Reading and decoding RDS (Radio Data System) in C#</title>
		<link>http://khason.net/blog/reading-and-decoding-rds-radio-data-system-in-c/</link>
		<comments>http://khason.net/blog/reading-and-decoding-rds-radio-data-system-in-c/#comments</comments>
		<pubDate>Fri, 12 Dec 2008 13:44:24 +0000</pubDate>
		<dc:creator>Tamir</dc:creator>
				<category><![CDATA[BLOG]]></category>
		<category><![CDATA[.NET 3.5]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[DEV]]></category>
		<category><![CDATA[Hardware]]></category>
		<category><![CDATA[Interop]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[Mobile]]></category>
		<category><![CDATA[source]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[Work process]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://khason.net/blog/reading-and-decoding-rds-radio-data-system-in-c/</guid>
		<description><![CDATA[RDS or Radio Data System is very common in US and many European countries. It is communication protocol used to send small amount of digital information using regular FM radio broadcast. This protocol is used to &#8220;tell&#8221; your receiver about alternative frequencies, time, program notifications, program types, traffic information and regular text (such as singer [...]

<p/>]]></description>
			<content:encoded><![CDATA[<p><a href="http://en.wikipedia.org/wiki/Radio_Data_System">RDS or Radio Data System</a> is very common in US and many European countries. It is communication protocol used to send small amount of digital information using regular FM radio broadcast. This protocol is used to &#8220;tell&#8221; your receiver about alternative frequencies, time, program notifications, program types, traffic information and regular text (such as singer name or genre). Unfortunately in Israel RDS is not very common and there is very limited number of radio stations broadcasts RDS information. </p>
<p><img border="0" alt="image" src="http://khason.net/images/2008/12/image-01bbfbdd-e89d-4a4e-89cd-e209655b5664.png" width="207" height="54"/> </p>
<h3>How RDS works?</h3>
<p>As mentioned earlier, it uses FM subcarrier to broadcast digital information. It was designed to support 10 and 18 characters numeric and 80 characters alphanumeric displays. RDS operates at 1187.5 bps and based on 26-bit word consisting of 16 data and 10 error detection bits. Due to the fact, that FM carrier is not very reliable, error code allows correct information to be received even if an error of 3-5 bits exists within 26 bit block. Each four data blocks interpreted as 104-bit signal and named &#8220;group&#8221;. Depending of the type of information, contained within the group, as different group type code is defined and transmitted within the group as upper five bits code. Even if more, then 104 bits required to completely send the information, there is no requirement that the next segment of the transmission be sent in the next group. There are 32 known groups types, defined by <a href="ftp://ftp.rds.org.uk/pub/acrobat/rbds1998.pdf">RFC</a>:</p>
<blockquote><p>private enum groupType : byte {<br />&nbsp;&nbsp; RDS_TYPE_0A = (0 * 2 + 0),<br />&nbsp;&nbsp; RDS_TYPE_0B = (0 * 2 + 1),<br />&nbsp;&nbsp; RDS_TYPE_1A = (1 * 2 + 0),<br />&nbsp;&nbsp; RDS_TYPE_1B = (1 * 2 + 1),<br />&nbsp;&nbsp; RDS_TYPE_2A = (2 * 2 + 0),<br />&nbsp;&nbsp; RDS_TYPE_2B = (2 * 2 + 1),<br />&nbsp;&nbsp; RDS_TYPE_3A = (3 * 2 + 0),<br />&nbsp;&nbsp; RDS_TYPE_3B = (3 * 2 + 1),<br />&nbsp;&nbsp; RDS_TYPE_4A = (4 * 2 + 0),<br />&nbsp;&nbsp; RDS_TYPE_4B = (4 * 2 + 1),<br />&nbsp;&nbsp; RDS_TYPE_5A = (5 * 2 + 0),<br />&nbsp;&nbsp; RDS_TYPE_5B = (5 * 2 + 1),<br />&nbsp;&nbsp; RDS_TYPE_6A = (6 * 2 + 0),<br />&nbsp;&nbsp; RDS_TYPE_6B = (6 * 2 + 1),<br />&nbsp;&nbsp; RDS_TYPE_7A = (7 * 2 + 0),<br />&nbsp;&nbsp; RDS_TYPE_7B = (7 * 2 + 1),<br />&nbsp;&nbsp; RDS_TYPE_8A = (8 * 2 + 0),<br />&nbsp;&nbsp; RDS_TYPE_8B = (8 * 2 + 1),<br />&nbsp;&nbsp; RDS_TYPE_9A = (9 * 2 + 0),<br />&nbsp;&nbsp; RDS_TYPE_9B = (9 * 2 + 1),<br />&nbsp;&nbsp; RDS_TYPE_10A = (10 * 2 + 0),<br />&nbsp;&nbsp; RDS_TYPE_10B = (10 * 2 + 1),<br />&nbsp;&nbsp; RDS_TYPE_11A = (11 * 2 + 0),<br />&nbsp;&nbsp; RDS_TYPE_11B = (11 * 2 + 1),<br />&nbsp;&nbsp; RDS_TYPE_12A = (12 * 2 + 0),<br />&nbsp;&nbsp; RDS_TYPE_12B = (12 * 2 + 1),<br />&nbsp;&nbsp; RDS_TYPE_13A = (13 * 2 + 0),<br />&nbsp;&nbsp; RDS_TYPE_13B = (13 * 2 + 1),<br />&nbsp;&nbsp; RDS_TYPE_14A = (14 * 2 + 0),<br />&nbsp;&nbsp; RDS_TYPE_14B = (14 * 2 + 1),<br />&nbsp;&nbsp; RDS_TYPE_15A = (15 * 2 + 0),<br />&nbsp;&nbsp; RDS_TYPE_15B = (15 * 2 + 1)<br />}</p>
</blockquote>
<p>Not all groups are in use all the time. However, there are some commitments, defined by the protocol. For example, 1A have to be transmitted at least once a second. This group contains special information, required for receivers to be synchronized and locked into the transmitting channel. </p>
<p>Within the error correction information we also receive the direction to treat them.</p>
<blockquote><p>private enum correctedType : byte {<br />&nbsp;&nbsp; NONE = 0,<br />&nbsp;&nbsp; ONE_TO_TWO = 1,<br />&nbsp;&nbsp; THREE_TO_FIVE = 2,<br />&nbsp;&nbsp; UNCORRECTABLE = 3<br />}</p>
</blockquote>
<p>Also, each message type has it own limits. For example RT (Radio Text &#8211; 64 character text to display on your receiver) and PS (Programme Service &#8211; eight character station identification) message are limited to 2 groups, when PI (Programme Identification &#8211; unique code of the station) and PTY (Programme Type &#8211; one of 31 predefined program types &#8211; e.g. News, Drama, Music) are limited to 4.</p>
<p>In addition to those constraints, block types are also different. But in this case, there are only 4 kinds</p>
<blockquote><p>private enum blockType : byte {<br />&nbsp;&nbsp; A = 6,<br />&nbsp;&nbsp; B = 4,<br />&nbsp;&nbsp; C = 2,<br />&nbsp;&nbsp; D = 0<br />}</p>
</blockquote>
<p>So, what we&#8217;re waiting for? Let&#8217;s start working.</p>
<h3>Handling errors</h3>
<p>First of all we should take care on errors and fix them if possible. For this purpose, we should first count them and detect the way of fixing</p>
<blockquote><p>var errorCount = (byte)((registers[0xa] &amp; 0x0E00) &gt;&gt; 9);<br />var errorFlags = (byte)(registers[0x6] &amp; 0xFF);<br />if (errorCount &lt; 4) {<br />&nbsp;&nbsp; _blocksValid += (byte)(4 &#8211; errorCount);<br />} else { /*drop data on more errors*/ return; } </p>
</blockquote>
<p>Once it done, we can try to fix them</p>
<blockquote><p>//Also drop the data if more than two errors were corrected<br />if (_getErrorsCorrected(errorFlags, blockType.B) &gt; correctedType.ONE_TO_TWO) return;</p>
<p>private correctedType _getErrorsCorrected(byte data, blockType block) { return (correctedType)((data &gt;&gt; (byte)block) &amp; 0&#215;30); }</p>
</blockquote>
<p>Now, our registers should be fine and we can start the detection of group type</p>
<h3>Group Type Detection</h3>
<p>This is very simple task, all we have to do is to get five upper bites to get a type and version.</p>
<blockquote><p>var group_type = (groupType)(registers[0xD] &gt;&gt; 11);</p>
</blockquote>
<p>Then we can handle PI and PTY, which we always have in RDS.</p>
<h3>PI and PTY treatment</h3>
<p>Now, let&#8217;s update pi code, due to the fact, that B format always have PI in words A and C</p>
<blockquote><p>_updatePI(registers[0xC]);  </p>
<p>if (((byte)group_type &amp; 0&#215;01) != 0) {<br /> _updatePI(registers[0xE]);<br />}</p>
</blockquote>
<p>To update PI, we should check whether the new value is different from the previous and update it only in case it changed.<br />
<blockquote>
<p>private void _updatePI(byte pi) {<br />&nbsp;&nbsp; uint rds_pi_validate_count = 0;<br />&nbsp;&nbsp; uint rds_pi_nonvalidated = 0;  </p>
<p>&nbsp;&nbsp; // if the pi value is the same for a certain number of times, update a validated pi variable<br />&nbsp;&nbsp; if (rds_pi_nonvalidated != pi) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rds_pi_nonvalidated = pi;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rds_pi_validate_count = 1;<br />&nbsp;&nbsp; } else {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rds_pi_validate_count++;<br />&nbsp;&nbsp; }  </p>
<p>&nbsp;&nbsp; if (rds_pi_validate_count &gt; PI_VALIDATE_LIMIT) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _piDisplay = rds_pi_nonvalidated;<br />&nbsp;&nbsp; }<br />}</p>
</blockquote>
<p>Then we will update PTY<br />
<blockquote>
<p>_updatePTY((byte)((registers[0xd] &gt;&gt; 5) &amp; 0x1f));</p>
</blockquote>
<p>PTY treatment is very similar to PI, however it can be multiplied.&nbsp;<br />
<blockquote>
<p>private void _updatePTY(byte pty) {<br />&nbsp;&nbsp; uint rds_pty_validate_count = 0;<br />&nbsp;&nbsp; uint rds_pty_nonvalidated = 0;  </p>
<p>&nbsp;&nbsp; // if the pty value is the same for a certain number of times, update a validated pty variable<br />&nbsp;&nbsp; if (rds_pty_nonvalidated != pty) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rds_pty_nonvalidated = pty;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rds_pty_validate_count = 1;<br />&nbsp;&nbsp; } else {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rds_pty_validate_count++;<br />&nbsp;&nbsp; }  </p>
<p>&nbsp;&nbsp; if (rds_pty_validate_count &gt; PTY_VALIDATE_LIMIT) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _ptyDisplay = rds_pty_nonvalidated;<br />&nbsp;&nbsp; }<br />}</p>
</blockquote>
<p>When we done with those two groups, we can start handling another. Today, we&#8217;ll handle only 0B, 2A and 2B types (I have a good reason for it, due to the fact, that only those are supported in Israel by now <img src='http://khason.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  ) So,<br />
<h3>Handling PS and different RTs</h3>
</p>
<p>Simple switch on those groups</p>
<blockquote><p>switch (group_type) {<br />&nbsp;&nbsp; case groupType.RDS_TYPE_0B:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; addr = (byte)((registers[0xd] &amp; 0&#215;3) * 2);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _updatePS((byte)(addr + 0), (byte)(registers[0xf] &gt;&gt; 8));<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _updatePS((byte)(addr + 1), (byte)(registers[0xf] &amp; 0xff));<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;<br />&nbsp;&nbsp; case groupType.RDS_TYPE_2A:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; addr = (byte)((registers[0xd] &amp; 0xf) * 4);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; abflag = (byte)((registers[0xb] &amp; 0&#215;0010) &gt;&gt; 4);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _updateRT(abflag, 4, addr, (byte[])registers.Skip(0xe), errorFlags);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;<br />&nbsp;&nbsp; case groupType.RDS_TYPE_2B:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; addr = (byte)((registers[0xd] &amp; 0xf) * 2);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; abflag = (byte)((registers[0xb] &amp; 0&#215;0010) &gt;&gt; 4);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // The last 32 bytes are unused in this format<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _rtTmp0[32] = 0x0d;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _rtTmp1[32] = 0x0d;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _rtCnt[32] = RT_VALIDATE_LIMIT;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _updateRT(abflag, 2, addr, (byte[])registers.Skip(0xe), errorFlags);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;<br />}</p>
</blockquote>
<p>and let&#8217;s dig into PS.</p>
<p>In PS, we have high and low probability bits. So, if new bit in sequence matches the high probability bite and we have recieved enough bytes to max out the counter, we&#8217;ll push it into the low probability array.</p>
<blockquote><p>if (_psTmp0[idx] == default(byte)) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (_psCnt[idx] &lt; PS_VALIDATE_LIMIT) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _psCnt[idx]++;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } else {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _psCnt[idx] = PS_VALIDATE_LIMIT;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _psTmp1[idx] = default(byte);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } </p>
</blockquote>
<p>Else, if new byte matches with the low probability byte, we should swap them and then reset the counter, by flagging the text as in transition. </p>
<blockquote><p>else if (_psTmp1[idx] == default(byte)) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (_psCnt[idx] &gt;= PS_VALIDATE_LIMIT) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; isTextChange = true;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _psCnt[idx] = PS_VALIDATE_LIMIT + 1;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _psTmp1[idx] = _psTmp0[idx];<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _psTmp0[idx] = default(byte);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } </p>
</blockquote>
<p>When we have an empty byte in high probability array or new bytes does not match anything we know, we should put it into low probability array.</p>
<blockquote><p>else if (_psCnt[idx] == null) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _psTmp0[idx] = default(byte);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _psCnt[idx] = 1;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } else {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _psTmp1[idx] = default(byte);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } </p>
</blockquote>
<p>Now, if we marked our text as changed, we should decrement the count for all characters to prevent displaying of partical message, which in still in transition.</p>
<blockquote><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (isTextChange) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (byte i = 0; i &lt; _psCnt.Length; i++) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (_psCnt[i] &gt; 1) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _psCnt[i]&#8211;;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } </p>
</blockquote>
<p>Then by checking PS text for incompetence, when there are characters in high probability array has been seen fewer times, that was limited by validation.<br />
<blockquote>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (byte i = 0; i &lt; _psCnt.Length; i++) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (_psCnt[i] &lt; PS_VALIDATE_LIMIT) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; isComplete = false;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } </p>
</blockquote>
<p>Only if PS text in the high probability array is complete, we&#8217;ll copy it into display.</p>
<blockquote><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (isComplete) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (byte i = 0; i &lt; _psDisplay.Length; i++) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _psDisplay[i] = _psTmp0[i];<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
</blockquote>
<p>It is not very hard to treat PS. Isn&#8217;t it? Let&#8217;s see what&#8217;s going on with RT.</p>
<p>If A and B message flag changes, we&#8217;ll try to force a display by increasing the validation count for each byte. Then, we&#8217;ll wipe any cached text.</p>
<blockquote><p>&nbsp;&nbsp; if (abFlag != _rtFlag &amp;&amp; _rtFlagValid) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // If the A/B message flag changes, try to force a display<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // by increasing the validation count of each byte<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (i = 0; i &lt; _rtCnt.Length; i++) _rtCnt[addr + i]++;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _updateRTValue();  </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Wipe out the cached text <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (i = 0; i &lt; _rtCnt.Length; i++) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _rtCnt[i] = 0;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _rtTmp0[i] = 0;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _rtTmp1[i] = 0;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp; }</p>
</blockquote>
<p>Now A and B flags are safe, sp we can start with message processing. First of all, NULL in RDS means space <img src='http://khason.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<blockquote><p>&nbsp;&nbsp; _rtFlag = abFlag;&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp; _rtFlagValid = true;&nbsp;&nbsp;&nbsp; </p>
<p>&nbsp;&nbsp; for (i = 0; i &lt; count; i++) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (p[i] == null) p[i] = (byte)&#8217; &#8216;; </p>
</blockquote>
<p>The new byte matches the high probability byte also in this case. We habe to recieve this bite enough to max out counters. Then we can push it into the low probability as well.<br />
<blockquote>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (_rtTmp0[addr + i] == p[i]) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (_rtCnt[addr + i] &lt; RT_VALIDATE_LIMIT) _rtCnt[addr + i]++;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _rtCnt[addr + i] = RT_VALIDATE_LIMIT;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _rtTmp1[addr + i] = p[i];<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } </p>
</blockquote>
<p>When the new byte matches with low probability byte, we&#8217;ll swap them as well and reset counters to update text in transition flag. However in this case, our counter will go higher, then the validation limit. So we&#8217;ll have to remove it down later.</p>
<blockquote><p>else if (_rtTmp1[addr + i] == p[i]) { </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (_rtCnt[addr + i] &gt;= PS_VALIDATE_LIMIT) isChange = true;  </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _rtCnt[addr + i] = RT_VALIDATE_LIMIT + 1;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _rtTmp1[addr + i] = _rtTmp0[addr + i];<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _rtTmp0[addr + i] = p[i];<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } </p>
</blockquote>
<p>Now, the new byte is replaced an empty byte in the high probability array. Also, if this byte does not match anything, we should move it into low probability.</p>
<blockquote><p>else if (_rtCnt[addr + i] == null) { <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _rtTmp0[addr + i] = p[i];<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _rtCnt[addr + i] = 1;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } else _rtTmp1[addr + i] = p[i]; </p>
<p>&nbsp;&nbsp; } </p>
</blockquote>
<p>Now when the text is changing, we&#8217;ll decrement the counter for all characters exactly as we did for PS.<br />
<blockquote>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (i = 0; i &lt; _rtCnt.Length; i++) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (_rtCnt[i] &gt; 1) _rtCnt[i]&#8211;;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp; } </p>
</blockquote>
<p>However, right after, we&#8217;ll update display.&nbsp; </p>
<blockquote><p>&nbsp;&nbsp; _updateRTValue();<br />}</p>
</blockquote>
<h3>Displaying RT</h3>
<p>But how to convert all those byte arrays into readable message? Simple <img src='http://khason.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>First of all if text is incomplete, we should keep loading it. Also it makes sense to check whether the target array is shorter then maximum allowed to prevent junk from being displayed.</p>
<blockquote><p>for (i = 0; i &lt; _rtTmp0.Length; i++) {<br />&nbsp;&nbsp; if (_rtCnt[i] &lt; RT_VALIDATE_LIMIT) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; isComplete = false;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;<br />&nbsp;&nbsp; }<br />&nbsp;&nbsp; if (_rtTmp0[i] == 0x0d) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;<br />&nbsp;&nbsp; }<br />} </p>
</blockquote>
<p>Now, when our Radio Text is in the high probability and it complete, we should copy buffers.</p>
<blockquote><p>if (isComplete) {<br />&nbsp;&nbsp; _Text = string.Empty; </p>
<p>&nbsp;&nbsp; for (i = 0; i &lt; _rtDisplay.Length; i += 2) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ((_rtDisplay[i] != 0x0d) &amp;&amp; (_rtDisplay[i + 1] != 0x0d)) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _rtDisplay[i] = _rtTmp0[i + 1];<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _rtDisplay[i + 1] = _rtTmp0[i];<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } else {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _rtDisplay[i] = _rtTmp0[i];<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _rtDisplay[i + 1] = _rtTmp0[i + 1];<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }  </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (_rtDisplay[i] != 0x0d)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _Text += _rtDisplay[i];  </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (_rtDisplay[i + 1] != 0x0d)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _Text += _rtDisplay[i + 1];  </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ((_rtDisplay[i] == 0x0d) || (_rtDisplay[i + 1] == 0x0d))<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; i = (byte)_rtDisplay.Length;<br />&nbsp;&nbsp; } </p>
</blockquote>
<p>And not forget to wipe out everything after the end of the message <img src='http://khason.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<blockquote><p>&nbsp;&nbsp; for (i++; i &lt; _rtDisplay.Length; i++) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _rtDisplay[i] = 0;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _rtCnt[i] = 0;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _rtTmp0[i] = 0;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _rtTmp1[i] = 0;<br />&nbsp;&nbsp; }<br />} </p>
</blockquote>
<p>And finally update the text</p>
<blockquote><p>Text = _Text;</p>
</blockquote>
<p>We done. Now we can handle RDS digital messages, but what to do with analog data we get? Don&#8217;t you already know? I <a href="http://khason.net/blog/sound-tone-and-dtmf-generation-by-using-managed-directsound-and-c-and-sine-tone-detection-with-pure-managed-goertzel-algorithm-implementation/">blogged about it here</a>. </p>
<p>Have a nice day and be good people, because you know how to write client, knows to get and parse radio data in managed code. </p>
<p><img border="0" alt="image" src="http://khason.net/images/2008/12/image-988a1f1f-0dec-46b1-b494-3c75a0b9ce7d.png" width="263" height="44"/></p>


<p/>]]></content:encoded>
			<wfw:commentRss>http://khason.net/blog/reading-and-decoding-rds-radio-data-system-in-c/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>WGS to UTM, UTM to WGS conversions, geo distance, azimuth and other geographical calculations in C#</title>
		<link>http://khason.net/blog/wgs-to-utm-utm-to-wgs-conversions-geo-distance-azimuth-and-other-geographical-calculations-in-c/</link>
		<comments>http://khason.net/blog/wgs-to-utm-utm-to-wgs-conversions-geo-distance-azimuth-and-other-geographical-calculations-in-c/#comments</comments>
		<pubDate>Mon, 15 Sep 2008 03:16:37 +0000</pubDate>
		<dc:creator>Tamir</dc:creator>
				<category><![CDATA[BLOG]]></category>
		<category><![CDATA[.NET 3.5]]></category>
		<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[GIS]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[source]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[Work process]]></category>

		<guid isPermaLink="false">http://khason.net/blog/wgs-to-utm-utm-to-wgs-conversions-geo-distance-azimuth-and-other-geographical-calculations-in-c/</guid>
		<description><![CDATA[&#8220;The reports of my death are greatly exaggerated&#8221; Since my post about leaving consulting field, I got huge amount of email with questions about all community projects, I&#8217;m leading, blogging in general and specific to the future of this blog. To make things clear, I leaved consulting, and now, I have less time and reasons [...]


Related posts:<ol><li><a href='http://khason.net/dev/inotifypropertychanged-auto-wiring-or-how-to-get-rid-of-redundant-code/' rel='bookmark' title='Permanent Link: INotifyPropertyChanged auto wiring or how to get rid of redundant code'>INotifyPropertyChanged auto wiring or how to get rid of redundant code</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<blockquote><h4><em>&#8220;The reports of my death are greatly exaggerated&#8221;</em></h4>
<p>Since <a href="http://khason.net/blog/im-leaving-consulting-field-joined-new-project-for-full-time/">my post about leaving consulting field</a>, I got huge amount of email with questions about all community projects, I&#8217;m leading, blogging in general and specific to the future of this blog. </p>
<p>To make things clear, I leaved consulting, and now, I have less time and reasons to blog, however, I&#8217;m keep blogging and maintaining almost all of my community projects (see the left side of the main page to list of most of those projects). Also, I try to answer all questions, I got via emails, however it&#8217;s too much to handle, thus be prepared for delays.</p>
<p>To be sure, I&#8217;m alive, you can <a href="http://twitter.com/tamir">follow me at twitter</a> (it demands less time to write) <img src='http://khason.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  Anyway, <strong>that you for reading my blog and supporting me</strong>.&nbsp; </p>
</blockquote>
<p>Now it&#8217;s good time to write something useful for you <img src='http://khason.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>If you ever wrote GIS programs, you, probably, know, that every time we forget how to convert latlon (Latitude-Longitude or, simpler World Geographical System) coordinates into Universal Transverse Mercator coordinates and vise verse, how to calculate geographical distance from point to point, line or segment to point and line to line, how to calculate azimuth between two geo points, or how to calculate destination point, based on start coordinate, distance and bearing. </p>
<p><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="318" alt="image" src="http://khason.net/images/2008/12/image-31.png" width="377" border="0"/> </p>
<p>In order to make our life easier, I decided to post number of methods in C#, I always use to perform such GIS calculations. I believe, that those classes will help you to perform your own geographical calculations. Also, do not forget two handy functions to convert degrees to radians and vv</p>
<blockquote><p>public static double DegToRad(double deg) { return (deg / 180.0 * Math.PI); } <br />public static double RadToDeg(double rad) { return (rad / Math.PI * 180.0); }</p>
</blockquote>
<p>Also, there are some constants should be used, if you&#8217;re calculating geo information in the Earth</p>
<blockquote><p>const double sm_a = 6378137.0; <br />const double sm_b = 6356752.314; <br />const double sm_EccSquared = 6.69437999013e-03; <br />const double UTMScaleFactor = 0.9996;</p>
</blockquote>
<h3>Conversion WGS2UTM (LatLon2UTM)</h3>
<p>Fist of all, we should calculate UTM zone. This one is simple</p>
<blockquote><p>int zone = (int)(Math.Floor((latlon.Longitude + 180.0) / 6) + 1);</p>
</blockquote>
<p>Now, when we have zone, we should calculate UTM central meridian, footprint of latitude and arc length of the meridian</p>
<blockquote><p>public static double UTMCentralMeridian(int zone) { return DegToRad(-183.0 + (zone * 6.0)); } </p>
<p>public static double FootpointLatitude(double y) { <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Precalculate n (Eq. 10.18) */ <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var n = (sm_a &#8211; sm_b) / (sm_a + sm_b); </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Precalculate alpha_ (Eq. 10.22) */ <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* (Same as alpha in Eq. 10.17) */ <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var alpha_ = ((sm_a + sm_b) / 2.0) * (1 + (Math.Pow(n, 2.0) / 4) + (Math.Pow(n, 4.0) / 64)); </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Precalculate y_ (Eq. 10.23) */ <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var y_ = y / alpha_; </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Precalculate beta_ (Eq. 10.22) */ <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var beta_ = (3.0 * n / 2.0) + (-27.0 * Math.Pow(n, 3.0) / 32.0) + (269.0 * Math.Pow(n, 5.0) / 512.0); </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Precalculate gamma_ (Eq. 10.22) */ <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var gamma_ = (21.0 * Math.Pow(n, 2.0) / 16.0) + (-55.0 * Math.Pow(n, 4.0) / 32.0); </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Precalculate delta_ (Eq. 10.22) */ <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var delta_ = (151.0 * Math.Pow(n, 3.0) / 96.0) + (-417.0 * Math.Pow(n, 5.0) / 128.0); </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Precalculate epsilon_ (Eq. 10.22) */ <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var epsilon_ = (1097.0 * Math.Pow(n, 4.0) / 512.0); </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Now calculate the sum of the series (Eq. 10.21) */ <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return y_ + (beta_ * Math.Sin(2.0 * y_)) + (gamma_ * Math.Sin(4.0 * y_)) + (delta_ * Math.Sin(6.0 * y_)) + (epsilon_ * Math.Sin(8.0 * y_)); <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } </p>
<p>public static double ArcLengthOfMeridian(double phi) { <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Precalculate n */ <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var n = (sm_a &#8211; sm_b) / (sm_a + sm_b); </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Precalculate alpha */ <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var alpha = ((sm_a + sm_b) / 2.0) * (1.0 + (Math.Pow(n, 2.0) / 4.0) + (Math.Pow(n, 4.0) / 64.0)); </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Precalculate beta */ <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var beta = (-3.0 * n / 2.0) + (9.0 * Math.Pow(n, 3.0) / 16.0) + (-3.0 * Math.Pow(n, 5.0) / 32.0); </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Precalculate gamma */ <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var gamma = (15.0 * Math.Pow(n, 2.0) / 16.0) + (-15.0 * Math.Pow(n, 4.0) / 32.0); </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Precalculate delta */ <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var delta = (-35.0 * Math.Pow(n, 3.0) / 48.0) + (105.0 * Math.Pow(n, 5.0) / 256.0); </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Precalculate epsilon */ <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var epsilon = (315.0 * Math.Pow(n, 4.0) / 512.0); </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Now calculate the sum of the series and return */ <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return alpha * (phi + (beta * Math.Sin(2.0 * phi)) + (gamma * Math.Sin(4.0 * phi)) + (delta * Math.Sin(6.0 * phi)) + (epsilon * Math.Sin(8.0 * phi))); <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
</blockquote>
<p>Now, we have everything to calculate UTM</p>
<blockquote><p>public static GeoPoint MapLatLonToXY(double phi, double lambda, double lambda0) { <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Precalculate ep2 */ <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var ep2 = (Math.Pow(sm_a, 2.0) &#8211; Math.Pow(sm_b, 2.0)) / Math.Pow(sm_b, 2.0); </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Precalculate nu2 */ <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var nu2 = ep2 * Math.Pow(Math.Cos(phi), 2.0); </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Precalculate N */ <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var N = Math.Pow(sm_a, 2.0) / (sm_b * Math.Sqrt(1 + nu2)); </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Precalculate t */ <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var t = Math.Tan(phi); <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var t2 = t * t; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var tmp = (t2 * t2 * t2) &#8211; Math.Pow(t, 6.0); </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Precalculate l */ <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var l = lambda &#8211; lambda0; </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Precalculate coefficients for l**n in the equations below <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; so a normal human being can read the expressions for easting <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; and northing <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212; l**1 and l**2 have coefficients of 1.0 */ <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var l3coef = 1.0 &#8211; t2 + nu2; </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var l4coef = 5.0 &#8211; t2 + 9 * nu2 + 4.0 * (nu2 * nu2); </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var l5coef = 5.0 &#8211; 18.0 * t2 + (t2 * t2) + 14.0 * nu2 &#8211; 58.0 * t2 * nu2; </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var l6coef = 61.0 &#8211; 58.0 * t2 + (t2 * t2) + 270.0 * nu2 &#8211; 330.0 * t2 * nu2; </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var l7coef = 61.0 &#8211; 479.0 * t2 + 179.0 * (t2 * t2) &#8211; (t2 * t2 * t2); </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var l8coef = 1385.0 &#8211; 3111.0 * t2 + 543.0 * (t2 * t2) &#8211; (t2 * t2 * t2); </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var xy = new GeoPoint(); <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Calculate easting (x) */ <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; xy.X = N * Math.Cos(phi) * l + (N / 6.0 * Math.Pow(Math.Cos(phi), 3.0) * l3coef * Math.Pow(l, 3.0)) + (N / 120.0 * Math.Pow(Math.Cos(phi), 5.0) * l5coef * Math.Pow(l, 5.0)) + (N / 5040.0 * Math.Pow(Math.Cos(phi), 7.0) * l7coef * Math.Pow(l, 7.0)); </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Calculate northing (y) */ <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; xy.Y = ArcLengthOfMeridian(phi) + (t / 2.0 * N * Math.Pow(Math.Cos(phi), 2.0) * Math.Pow(l, 2.0)) + (t / 24.0 * N * Math.Pow(Math.Cos(phi), 4.0) * l4coef * Math.Pow(l, 4.0)) + (t / 720.0 * N * Math.Pow(Math.Cos(phi), 6.0) * l6coef * Math.Pow(l, 6.0)) + (t / 40320.0 * N * Math.Pow(Math.Cos(phi), 8.0) * l8coef * Math.Pow(l, 8.0)); </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return xy; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } </p>
<p>public static GeoCoord MapXYToLatLon(double x, double y, double lambda0) { <br />&nbsp;&nbsp; /* Get the value of phif, the footpoint latitude. */ <br />&nbsp;&nbsp; double phif = FootpointLatitude(y); </p>
<p>&nbsp;&nbsp; /* Precalculate ep2 */ <br />&nbsp;&nbsp; double ep2 = (Math.Pow(sm_a, 2.0) &#8211; Math.Pow(sm_b, 2.0)) / Math.Pow(sm_b, 2.0); </p>
<p>&nbsp;&nbsp; /* Precalculate cos (phif) */ <br />&nbsp;&nbsp; var cf = Math.Cos(phif); </p>
<p>&nbsp;&nbsp; /* Precalculate nuf2 */ <br />&nbsp;&nbsp; var nuf2 = ep2 * Math.Pow(cf, 2.0); </p>
<p>&nbsp;&nbsp; /* Precalculate Nf and initialize Nfpow */ <br />&nbsp;&nbsp; var Nf = Math.Pow(sm_a, 2.0) / (sm_b * Math.Sqrt(1 + nuf2)); <br />&nbsp;&nbsp; var Nfpow = Nf; </p>
<p>&nbsp;&nbsp; /* Precalculate tf */ <br />&nbsp;&nbsp; var tf = Math.Tan(phif); <br />&nbsp;&nbsp; var tf2 = tf * tf; <br />&nbsp;&nbsp; var tf4 = tf2 * tf2; </p>
<p>&nbsp;&nbsp; /* Precalculate fractional coefficients for x**n in the equations <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; below to simplify the expressions for latitude and longitude. */ <br />&nbsp;&nbsp; var x1frac = 1.0 / (Nfpow * cf); </p>
<p>&nbsp;&nbsp; Nfpow *= Nf;&nbsp;&nbsp; /* now equals Nf**2) */ <br />&nbsp;&nbsp; var x2frac = tf / (2.0 * Nfpow); </p>
<p>&nbsp;&nbsp; Nfpow *= Nf;&nbsp;&nbsp; /* now equals Nf**3) */ <br />&nbsp;&nbsp; var x3frac = 1.0 / (6.0 * Nfpow * cf); </p>
<p>&nbsp;&nbsp; Nfpow *= Nf;&nbsp;&nbsp; /* now equals Nf**4) */ <br />&nbsp;&nbsp; var x4frac = tf / (24.0 * Nfpow); </p>
<p>&nbsp;&nbsp; Nfpow *= Nf;&nbsp;&nbsp; /* now equals Nf**5) */ <br />&nbsp;&nbsp; var x5frac = 1.0 / (120.0 * Nfpow * cf); </p>
<p>&nbsp;&nbsp; Nfpow *= Nf;&nbsp;&nbsp; /* now equals Nf**6) */ <br />&nbsp;&nbsp; var x6frac = tf / (720.0 * Nfpow); </p>
<p>&nbsp;&nbsp; Nfpow *= Nf;&nbsp;&nbsp; /* now equals Nf**7) */ <br />&nbsp;&nbsp; var x7frac = 1.0 / (5040.0 * Nfpow * cf); </p>
<p>&nbsp;&nbsp; Nfpow *= Nf;&nbsp;&nbsp; /* now equals Nf**8) */ <br />&nbsp;&nbsp; var x8frac = tf / (40320.0 * Nfpow); </p>
<p>&nbsp;&nbsp; /* Precalculate polynomial coefficients for x**n. <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212; x**1 does not have a polynomial coefficient. */ <br />&nbsp;&nbsp; var x2poly = -1.0 &#8211; nuf2; </p>
<p>&nbsp;&nbsp; var x3poly = -1.0 &#8211; 2 * tf2 &#8211; nuf2; </p>
<p>&nbsp;&nbsp; var x4poly = 5.0 + 3.0 * tf2 + 6.0 * nuf2 &#8211; 6.0 * tf2 * nuf2 &#8211; 3.0 * (nuf2 * nuf2) &#8211; 9.0 * tf2 * (nuf2 * nuf2); </p>
<p>&nbsp;&nbsp; var x5poly = 5.0 + 28.0 * tf2 + 24.0 * tf4 + 6.0 * nuf2 + 8.0 * tf2 * nuf2; </p>
<p>&nbsp;&nbsp; var x6poly = -61.0 &#8211; 90.0 * tf2 &#8211; 45.0 * tf4 &#8211; 107.0 * nuf2 + 162.0 * tf2 * nuf2; </p>
<p>&nbsp;&nbsp; var x7poly = -61.0 &#8211; 662.0 * tf2 &#8211; 1320.0 * tf4 &#8211; 720.0 * (tf4 * tf2); </p>
<p>&nbsp;&nbsp; var x8poly = 1385.0 + 3633.0 * tf2 + 4095.0 * tf4 + 1575 * (tf4 * tf2); </p>
<p>&nbsp;&nbsp; var philambda = new GeoCoord(); <br />&nbsp;&nbsp; /* Calculate latitude */ <br />&nbsp;&nbsp; philambda.Latitude = phif + x2frac * x2poly * (x * x) + x4frac * x4poly * Math.Pow(x, 4.0) + x6frac * x6poly * Math.Pow(x, 6.0) + x8frac * x8poly * Math.Pow(x, 8.0); </p>
<p>&nbsp;&nbsp; /* Calculate longitude */ <br />&nbsp;&nbsp; philambda.Longitude = lambda0 + x1frac * x + x3frac * x3poly * Math.Pow(x, 3.0) + x5frac * x5poly * Math.Pow(x, 5.0) + x7frac * x7poly * Math.Pow(x, 7.0); </p>
<p>&nbsp;&nbsp; return philambda; <br />}</p>
</blockquote>
<p>We done, the only thing, should be adjusted is easting and northing for UTM system</p>
<blockquote><p>xy.X = xy.X * UTMScaleFactor + 500000.0; <br />xy.Y = xy.Y * UTMScaleFactor; <br />if (xy.Y &lt; 0.0) xy.Y += 10000000.0;</p>
</blockquote>
<h3>Conversion UTM2WGS (UTM2LatLon)</h3>
<p>After al had all thin math in previous chapter, now we should calculate opposite conversion</p>
<p>First adjust</p>
<blockquote><p>x -= 500000.0; <br />x /= UTMScaleFactor; </p>
<p>/* If in southern hemisphere, adjust y accordingly. */ <br />if (southhemi) y -= 10000000.0; </p>
<p>y /= UTMScaleFactor; </p>
<p>var cmeridian = UTMCentralMeridian(zone);</p>
</blockquote>
<p>Now calculate</p>
<blockquote><p>public static GeoCoord MapXYToLatLon(double x, double y, double lambda0) { <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Get the value of phif, the footpoint latitude. */ <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; double phif = FootpointLatitude(y); </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Precalculate ep2 */ <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; double ep2 = (Math.Pow(sm_a, 2.0) &#8211; Math.Pow(sm_b, 2.0)) / Math.Pow(sm_b, 2.0); </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Precalculate cos (phif) */ <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var cf = Math.Cos(phif); </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Precalculate nuf2 */ <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var nuf2 = ep2 * Math.Pow(cf, 2.0); </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Precalculate Nf and initialize Nfpow */ <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var Nf = Math.Pow(sm_a, 2.0) / (sm_b * Math.Sqrt(1 + nuf2)); <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var Nfpow = Nf; </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Precalculate tf */ <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var tf = Math.Tan(phif); <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var tf2 = tf * tf; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var tf4 = tf2 * tf2; </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Precalculate fractional coefficients for x**n in the equations <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; below to simplify the expressions for latitude and longitude. */ <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var x1frac = 1.0 / (Nfpow * cf); </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Nfpow *= Nf;&nbsp;&nbsp; /* now equals Nf**2) */ <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var x2frac = tf / (2.0 * Nfpow); </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Nfpow *= Nf;&nbsp;&nbsp; /* now equals Nf**3) */ <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var x3frac = 1.0 / (6.0 * Nfpow * cf); </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Nfpow *= Nf;&nbsp;&nbsp; /* now equals Nf**4) */ <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var x4frac = tf / (24.0 * Nfpow); </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Nfpow *= Nf;&nbsp;&nbsp; /* now equals Nf**5) */ <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var x5frac = 1.0 / (120.0 * Nfpow * cf); </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Nfpow *= Nf;&nbsp;&nbsp; /* now equals Nf**6) */ <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var x6frac = tf / (720.0 * Nfpow); </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Nfpow *= Nf;&nbsp;&nbsp; /* now equals Nf**7) */ <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var x7frac = 1.0 / (5040.0 * Nfpow * cf); </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Nfpow *= Nf;&nbsp;&nbsp; /* now equals Nf**8) */ <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var x8frac = tf / (40320.0 * Nfpow); </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Precalculate polynomial coefficients for x**n. <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212; x**1 does not have a polynomial coefficient. */ <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var x2poly = -1.0 &#8211; nuf2; </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var x3poly = -1.0 &#8211; 2 * tf2 &#8211; nuf2; </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var x4poly = 5.0 + 3.0 * tf2 + 6.0 * nuf2 &#8211; 6.0 * tf2 * nuf2 &#8211; 3.0 * (nuf2 * nuf2) &#8211; 9.0 * tf2 * (nuf2 * nuf2); </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var x5poly = 5.0 + 28.0 * tf2 + 24.0 * tf4 + 6.0 * nuf2 + 8.0 * tf2 * nuf2; </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var x6poly = -61.0 &#8211; 90.0 * tf2 &#8211; 45.0 * tf4 &#8211; 107.0 * nuf2 + 162.0 * tf2 * nuf2; </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var x7poly = -61.0 &#8211; 662.0 * tf2 &#8211; 1320.0 * tf4 &#8211; 720.0 * (tf4 * tf2); </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var x8poly = 1385.0 + 3633.0 * tf2 + 4095.0 * tf4 + 1575 * (tf4 * tf2); </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var philambda = new GeoCoord(); <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Calculate latitude */ <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; philambda.Latitude = phif + x2frac * x2poly * (x * x) + x4frac * x4poly * Math.Pow(x, 4.0) + x6frac * x6poly * Math.Pow(x, 6.0) + x8frac * x8poly * Math.Pow(x, 8.0); </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Calculate longitude */ <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; philambda.Longitude = lambda0 + x1frac * x + x3frac * x3poly * Math.Pow(x, 3.0) + x5frac * x5poly * Math.Pow(x, 5.0) + x7frac * x7poly * Math.Pow(x, 7.0); </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return philambda; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
</blockquote>
<p>At the end do not forget to return Latitude and Longitude in degrees, rather, then in radians</p>
<h3>Azimuth calculation</h3>
<p>We done with coordinates, now azimuth</p>
<blockquote><p>public static double GetAzimuth(WGSCoord c1, WGSCoord c2) { <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var lat1 = DegToRad(c1.Latitude); <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var lon1 = DegToRad(c1.Longitude); <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var lat2 = DegToRad(c2.Latitude); <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var lon2 = DegToRad(c2.Longitude); </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return RadToDeg(Math.Asin(Math.Sin(lon1 &#8211; lon2) * Math.Cos(lat2) / Math.Sin(Math.Acos(Math.Sin(lat2) * Math.Sin(lat1) + Math.Cos(lat1) * Math.Cos(lat2) * Math.Cos(lon2 &#8211; lon1))))); <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
</blockquote>
<h3>Distance calculations</h3>
<blockquote><p>public static double GetDistance(WGSCoord c1, WGSCoord c2) { <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var dLat = DegToRad(c2.Latitude &#8211; c1.Latitude); <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var dLon = DegToRad(c2.Longitude &#8211; c2.Longitude); <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + Math.Cos(DegToRad(c1.Latitude)) * Math.Cos(DegToRad(c2.Latitude)) * Math.Sin(dLon / 2) * Math.Sin(dLon / 2); <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 &#8211; a)); <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return sm_a * c; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
</blockquote>
<h3>Final destination coordinates, based on start coordinate, bearing and distance</h3>
<blockquote><p>public static WGSCoord GetDestination(WGSCoord start, double distance, double azimuth) { <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var lat = RadToDeg(Math.Asin(Math.Sin(DegToRad(start.Latitude)) * Math.Cos(DegToRad(distance / sm_a)) + Math.Cos(DegToRad(start.Latitude)) * Math.Sin(DegToRad(distance / sm_a)) * Math.Cos(DegToRad(azimuth)))); <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var lon = start.Longitude + DegToRad(Math.Atan2(Math.Sin(DegToRad(azimuth)) * Math.Sin(DegToRad(distance / sm_a)) * Math.Cos(DegToRad(start.Latitude)), Math.Cos(DegToRad(distance / sm_a)) &#8211; Math.Sin(DegToRad(start.Latitude)) * Math.Sin(DegToRad(lat)))); <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return new WGSCoord(lon,lat); </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
</blockquote>
<h3>Midpoint calculation</h3>
<blockquote><p>public static WGSCoord GetMidpoint(WGSCoord start, WGSCoord end) { <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var dLat = DegToRad(end.Latitude &#8211; start.Latitude); <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var dLon = DegToRad(end.Longitude &#8211; start.Longitude); <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var lat2 = DegToRad(end.Latitude); <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var lat1 = DegToRad(start.Latitude); <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var lon1 = DegToRad(start.Longitude); </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var Bx = Math.Cos(lat2) * Math.Cos(dLon); <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var By = Math.Cos(lat2) * Math.Sin(dLon); <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var lat3 = Math.Atan2(Math.Sin(lat1) + Math.Sin(lat2), Math.Sqrt((Math.Cos(lat1) + Bx) * (Math.Cos(lat1) + Bx) + By * By)); <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var lon3 = lon1 + Math.Atan2(By, Math.Cos(lat1) + Bx); <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return new WGSCoord(RadToDeg(lon3), RadToDeg(lat3)); <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
</blockquote>
<h3>Rhumb lines calculation</h3>
<p>First of all, what is rhumb lines? Rhumb lines or loxodrome is a path of constant bearing, which crosses all meridians at the same angle. This calculation is very useful, if you want to follow constant compass bearing, instead of continually adjustment of it.</p>
<blockquote><p>public static double GetRhumbDistance(WGSCoord start, WGSCoord end) { <br />&nbsp;&nbsp; var dLat = DegToRad(end.Latitude &#8211; start.Latitude); <br />&nbsp;&nbsp; var dLon = DegToRad(end.Longitude &#8211; start.Longitude); <br />&nbsp;&nbsp; var lat1 = DegToRad(start.Latitude); <br />&nbsp;&nbsp; var lon1 = DegToRad(start.Longitude); <br />&nbsp;&nbsp; var lat2 = DegToRad(end.Latitude); <br />&nbsp;&nbsp; var lon2 = DegToRad(end.Longitude); </p>
<p>&nbsp;&nbsp; var dPhi = Math.Log(Math.Tan(lat2 / 2 + Math.PI / 4) / Math.Tan(lat1 / 2 + Math.PI / 4)); <br />&nbsp;&nbsp; var q = (Math.Abs(dLat) &gt; 1e-10) ? dLat / dPhi : Math.Cos(lat1); <br />&nbsp;&nbsp; if (Math.Abs(dLon) &gt; Math.PI) { <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dLon = dLon &gt; 0 ? -(2 * Math.PI &#8211; dLon) : (2 * Math.PI + dLon); <br />&nbsp;&nbsp; } <br />&nbsp;&nbsp; return Math.Sqrt(dLat * dLat + q * q * dLon * dLon) * sm_a; <br />}</p>
<p>public static double GetRhumbBearing(WGSCoord start, WGSCoord end) { <br />&nbsp;&nbsp; var dLat = DegToRad(end.Latitude &#8211; start.Latitude); <br />&nbsp;&nbsp; var dLon = DegToRad(end.Longitude &#8211; start.Longitude); </p>
<p>&nbsp;&nbsp; var lat1 = DegToRad(start.Latitude); <br />&nbsp;&nbsp; var lon1 = DegToRad(start.Longitude); <br />&nbsp;&nbsp; var lat2 = DegToRad(end.Latitude); <br />&nbsp;&nbsp; var lon2 = DegToRad(end.Longitude); </p>
<p>&nbsp;&nbsp; var dPhi = Math.Log(Math.Tan(lat2 / 2 + Math.PI / 4) / Math.Tan(lat1 / 2 + Math.PI / 4)); <br />&nbsp;&nbsp; if (Math.Abs(dLon) &gt; Math.PI) { <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dLon = dLon &gt; 0 ? -(2 * Math.PI &#8211; dLon) : (2 * Math.PI + dLon); <br />&nbsp;&nbsp; } <br />&nbsp;&nbsp; return Math.Atan2(dLon, dPhi); <br />}</p>
<p>public static WGSCoord GetRhumbDestination(WGSCoord start, WGSCoord end) { <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var lat1 = DegToRad(start.Latitude); <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var lon1 = DegToRad(start.Longitude); <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var lat2 = DegToRad(end.Latitude); <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var lon2 = DegToRad(end.Longitude); </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var d = GetRhumbDistance(start, end); <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var b = GetRhumbBearing(start, end); <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lat2 = lat1 +&nbsp; d * Math.Cos(b); <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var dPhi = Math.Log(Math.Tan(lat2 / 2 + Math.PI / 4) / Math.Tan(lat1 / 2 + Math.PI / 4)); <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var q = (Math.Abs(lat2 &#8211; lat1) &gt; 1e-10) ? (lat2 &#8211; lat1) / dPhi : Math.Cos(lat1); <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var dLon = d * Math.Sin(b) / q; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (Math.Abs(lat2) &gt; Math.PI / 2) lat2 = lat2 &gt; 0 ? Math.PI &#8211; lat2 : -Math.PI &#8211; lat2; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lon2 = (lon1 + dLon + Math.PI) % (2 * Math.PI) &#8211; Math.PI; </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return new WGSCoord(RadToDeg(lon2), RadToDeg(lat2)); <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
</blockquote>
<h3>Conversion between decimal degrees to degrees-minutes-seconds</h3>
<blockquote><p>public static Pair&lt;double,double&gt; ToD_Mm(this double decCoord) { <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var degSeg = (int)decCoord; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return new Pair&lt;double, double&gt;(degSeg, decCoord % degSeg * 60); <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
</blockquote>
<p>Now it&#8217;s good time for small math (if there were not enough until now) <img src='http://khason.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  All following methods are not geo spatial oriented. All those are only geometry. The only helper method, you need here is Dot vector operation</p>
<blockquote><p>private static double dot(Vector v1, Vector v2) { return (v1.X * v2.X + v1.Y * v2.Y); }</p>
</blockquote>
<h3>Get the distance between two points</h3>
<blockquote><p>private static double distance(Point p1, Point p2) { <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var a = Math.Pow(p1.X, 2) &#8211; Math.Pow(p2.X, 2); <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var b = Math.Pow(p1.Y, 2) &#8211; Math.Pow(p2.Y, 2); <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return Math.Sqrt(Math.Abs(a + b)); <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
</blockquote>
<h3>Get the shortest distance between point and line segment</h3>
<blockquote><p>private static double distance(Point p1, Point p2, Point p) { <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var v = p1 &#8211; p2; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var w = p &#8211; p2; </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var c1 = dot(w, v); <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (c1 &lt;= 0) return distance(p, p1); </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var c2 = dot(v, v); <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (c2 &lt;= c1) return distance(p, p2); </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var b = c1 / c2; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Point Pb = p1 + b * v; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return distance(p, Pb); <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
</blockquote>
<p>Isn&#8217;t it enough math for now? It is. Have a nice day and be good people. Next time, we&#8217;ll speak about different path finding algorithms, edges, nodes and other fun math optimizations.</p>


<p>Related posts:<ol><li><a href='http://khason.net/dev/inotifypropertychanged-auto-wiring-or-how-to-get-rid-of-redundant-code/' rel='bookmark' title='Permanent Link: INotifyPropertyChanged auto wiring or how to get rid of redundant code'>INotifyPropertyChanged auto wiring or how to get rid of redundant code</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://khason.net/blog/wgs-to-utm-utm-to-wgs-conversions-geo-distance-azimuth-and-other-geographical-calculations-in-c/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
