title: How to check two point intersect each other if lat,long and azimuth is given?
body: <p>I have two type of site like site A and site B.each site have own lat, long and azimuth.</p>
  <p>I need to check if site A is intersect B in below condition.</p>
  <ol>
  <li>if Site A is looking to B</li>
  <li>if first step is true draw a tringle of +35 degree and -35 degree of azimuth A and B of distance. e.g suppose distance between site A and B is 5 Km. then tringle would be of 5Km with +35 degree and -35degree of the angle for both site A and B.</li>
  <li>need to check both tringle for site A and site B intersect each other or not.</li>
  </ol>
  <p>I have the below data :</p>
  <pre><code>+----------+-----------+--------------+----------+-----------+--------------+
  |source_lat|source_long|source_azimuth|target_lat|target_long|target_azimuth|
  +----------+-----------+--------------+----------+-----------+--------------+
  |18.3965   |74.7274    |20            |18.431    |74.7778    |245           |
  |18.3965   |74.7274    |320           |18.44394  |74.72676   |170           |
  |18.45306  |74.67871   |70            |18.44394  |74.72676   |170           |
  |18.45306  |74.67871   |160           |18.44394  |74.72676   |170           |
  |18.45306  |74.67871   |150           |18.44394  |74.72676   |170           |
  |18.3965   |74.7274    |20            |18.40797  |74.65001   |70            |
  |18.3965   |74.7274    |260           |18.45008  |74.81401   |270           |
  |18.45306  |74.67871   |70            |18.51339  |74.70244   |90            |
  |18.3965   |74.7274    |260           |18.44394  |74.72676   |170           |
  |18.3965   |74.7274    |320           |18.44394  |74.72676   |170           |
  |18.45306  |74.67871   |280           |18.40797  |74.65001   |30            |
  |18.45306  |74.67871   |290           |18.40797  |74.65001   |30            |
  +----------+-----------+--------------+----------+-----------+--------------+
  </code></pre>
  <p>Here i am using scala programming ,
  I have used below code to calculate the distance and bearing.</p>
  <pre><code>/*
  * That function takes lat and long of site A and Site B 
  * @return concate of distance and bearing like 
  * suppose distance is 2 (KM) and bearing is 120 then return 2_120.
  * later i split that above string by underscore &quot;_&quot; to get distance and bearing.
  */   
    private val AVERAGE_RADIUS_OF_EARTH_KM = 6371
     
  def calculateDistanceInKilometer(userLat1: String,userLong1: String, wareHouseLat1:String,wareHouseLong1 : String): Option[String] = {
          if ((userLat1 != null) &amp;&amp; (userLong1 != null) &amp;&amp; (wareHouseLat1 != null) &amp;&amp; (wareHouseLong1 != null)) {
            val latDistance = Math.toRadians(userLat1.toDouble - wareHouseLat1.toDouble)
            val lngDistance = Math.toRadians(userLong1.toDouble - wareHouseLong1.toDouble)
            val sinLat = Math.sin(latDistance / 2)
            val sinLng = Math.sin(lngDistance / 2)
            val a = sinLat * sinLat +
              (Math.cos(Math.toRadians(userLat1.toDouble)) *
                Math.cos(Math.toRadians(wareHouseLat1.toDouble)) *
                sinLng * sinLng)
            val c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a))
            var lat1 = Math.toRadians(userLat1.toDouble)
            var lng1 = Math.toRadians(userLong1.toDouble)
            var lat2 = Math.toRadians(wareHouseLat1.toDouble)
            var lng2 = Math.toRadians(wareHouseLong1.toDouble)
            //Source//Source
            val dLon = lng2 - lng1
            val y = Math.sin(dLon) * Math.cos(lat2)
            val x = Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1) * Math.cos(lat2) * Math.cos(dLon)
            var brng = Math.toDegrees(Math.atan2(y, x))
            brng = (brng + 360) % 360
            val distance = (AVERAGE_RADIUS_OF_EARTH_KM * c).toDouble
            val final_data = distance.toString + &quot;_&quot; + brng.toString
            Some(final_data)
          }else{
            None
          }
        }
  </code></pre>
  <blockquote>
  <p>and after getting the distance and bearing i have split and get
  distance and bearing. and give column name as distance,
  azimuth_between.</p>
  </blockquote>
  <p>Now i have used to below logic to check weather site A and site B interset each other like</p>
  <pre><code>//.withColumn(&quot;matched&quot;,expr(&quot;case when ((target_azimuth &lt;= (azimuth_between + 270) and target_azimuth &gt;= (azimuth_between + 90)) or ((target_azimuth+360) &lt;= (azimuth_between + 270) and (target_azimuth+360) &gt;= (azimuth_between + 90))) then 'intersect' else 'Not intersect'&quot;))
  
  
  case when ((target_azimuth &lt;= (azimuth_between + 270) and target_azimuth &gt;= (azimuth_between + 90)) or ((target_azimuth+360) &lt;= (azimuth_between + 270) and (target_azimuth+360) &gt;= (azimuth_between + 90))) then 'intersect' else 'Not intersect'
  </code></pre>
  <p>The above code gives the correct that weather site A interset site B but it is not restricted to distance(getting distance between two site a and site b), that must be checked only within distance . sometime site A intersect site B after 10km,20km but it must be interset the distance which is the actual distance.</p>
  <p>Please check wheather we can solve this problem in either Java,Scala,Mysql,PostGresSql etc.</p>