DEV Community

Muhammad Khodjaev
Muhammad Khodjaev

Posted on • Edited on

Koordinatalar orasidagi masofani hisoblovchi dastur

Ushbu dasturda Haversine formulasi orqali ikki koordinata orasidagi masofani (metrda) aniqlanadi. Loyihaning ahamiyatli qismi shundaki u struct ya'ni value type bilan birga qurilgan.

Dasturga kirish qismi:

O'zimiz joylashgan koordinatani fixed qilib ya'ni o'zgarmas qilib e'lon qilib olamiz. Bu uchun yordamga bizga Coordinate struct'ining konstraktori yordam beradi. Property'larimiz { get; init; } bo'ladi. init bo'lishining sababi esa koordinata bir martta assign qilingandan so'ng boshqa uni o'zgartirish mumkin bo'lmasligi uchun init yordam beradi.

Foydalanuvchidan koordinatani latitude va longitude ko'rinishida orasida vergul bilan ajratib kiritish so'raladi. Agar foydalanuvchi boshqa narsa kiritsa - (please provide coordinates) deb error beradi. Koordinata kiritilgandan so'ng Struct joylashgan C-sharp faylining Coordinate degan struct'ning GetMetre metodi ishga tushadi.

GetMetre() esa bizning fixed koordinata bilan foydalanuvchining koordinatasini olib DistanceTo() metodiga ketadi va oradagi masofa bilan qaytib keladi. Oradagi masofa esa Program.cs ga keladi va Math.Round metodi bilan chiroyli ko'rinishga keladi. Shunday qilib, biz oradagi masofa qanchaligini bilib olsak bo'ladi.

var coordinate = new Coordinate(41.351040, 69.352936);
var coordinateFromUser = Console.ReadLine()?.Split(",") ?? [];

if (coordinateFromUser is {Length: 2})
    Console.WriteLine(Math.Round(coordinate.GetMetre(double.Parse(coordinateFromUser[0]), double.Parse(coordinateFromUser[1])), 2));
else
    Console.WriteLine("Please provide coordinates");
//----------------------------------------CoordinateToDistance.cs
public struct Coordinate(double latitude, double longitude)
{
    const double EarthRadius = 6371000;
    public double Latitude { get; init; } = latitude;
    public double Longtitude { get; init; } = longitude;

    public static double DistanceTo(double lat1, double lon1, double lat2, double lon2) 
   {
//formula of Haversine 
    }

public static double DegreesToRadians(double degrees) 
    {
    }

public double GetMetre(double lat, double lon) 
        => DistanceTo(lat, lon, Latitude, Longtitude);
}
Enter fullscreen mode Exit fullscreen mode

DEV Challenges are live now!

DEV Challenges Hub

Check out all the ways to participate, certify your skills, and win prizes.

Visit the Challenges Hub

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay