How to calculate euclidean distance between two stations - c#

I need to find the 2D distance between two stations. I need to create a station class that can be used to store a list of stations as objects, such as this:
class Station
{
public Station(string name, double X, double Y)
{
Name = name;
xcor = X;
ycor = Y;
}
public string Name {get; set;}
public double xcor {get; set;}
public double ycor {get; set;}
}
class Program
public static void Main(string[] args)
{
public List<Station> Stationlist = new List<Station>();
Stationlist.Add(new Station("po",1,1));
Stationlist.Add(new Station("wsx",200,200));
}
I need to create a distance method that will calculate the distance between these two stations by running it like this:
Console.WriteLine(Distance.euDistance(station[0], station[1]));
I have tried to create a method to calculate the euclidean distance but can't get it to successfully calculate the distance between the two stations. This is what I have created for my Distance method:
class Distance
{
public static double distanceTEST(Station current, Station next)
{
return Math.Sqrt((Math.Pow((current.Station.X - next.Station.X), 2) +
Math.Pow((current.Station.Y - next.Station.Y), 2) *
100000.0 / 100000.0) * 1);
}
}
I want to have it print a result such as this: (that is just an example)
Console.WriteLine("{0} -> {1} {2} meters, Name[0], Name[1], distance);
po -> wsx 56.6505106 meters

Maybe you can write a extension method for Station class.
class Program
{
static void Main(string[] args)
{
var station1 = new Station("po", -7, 17);
var station2 = new Station("wsx", -4, 6.5);
Console.WriteLine(station1.CalculateEuDistance(station2));
Console.ReadKey();
}
}
public class Station
{
public string Name { get; set; }
public double X { get; set; }
public double Y { get; set; }
public Station()
{
}
public Station(string name, double x, double y)
{
Name = name;
X = x;
Y = y;
}
}
public static class StationExtensions
{
public static double CalculateEuDistance(this Station currentStation, Station station)
{
return Math.Sqrt(Math.Pow(currentStation.Y - currentStation.X,2) + Math.Pow(station.Y - station.X,2));
}
}

Related

Printing some data

I have been trying to print the information about whether the equipment is mobile or immobile, but for some reason it isn't showing any output.
public class Equipment
{
public string name;
public string description;
public string type;
public int distance_moved = 0;
public int wheels = 0;
public int weight = 0;
public int maintenance_cost;
public Equipment(string n, string d, string t, int w, int wt)
{
n = name;
d = description;
t = type;
w = wheels;
wt = weight;
}
public void Move_By(int d)
{
distance_moved = distance_moved + d;
if (this.type == "Mobile")
{
maintenance_cost = wheels * distance_moved;
}
else maintenance_cost = weight * distance_moved;
}
}
class Program
{
static void Main(string[] args)
{
var Equipment_list = new List<Equipment>()
{
new Equipment("Bulldozer", "Light Vehicle", "Mobile",4,0),
new Equipment("Box", "Huge Box", "ImMobile",0,10),
new Equipment("Drill Machine", "Light Machine", "ImMobile",0,20),
new Equipment("Truck", "Heavy Vehicle", "Mobile",4,0)
};
var Mobile_Equipment = from Mobile in Equipment_list
where Mobile.type == "Mobile"
orderby Mobile.name
select Mobile;
foreach (var Mobile in Mobile_Equipment)
{
Console.WriteLine("{0} is a Mobile Equipment", Mobile.name);
}
Console.ReadKey();
}
}
Your Equipment constructor is doing all of its assignments backwards. You're assigning the default class member values to the method arguments. It should be like this instead:
public Equipment(string n, string d, string t, int w, int wt)
{
name = n;
description = d;
type = t;
wheels = w;
weight = wt;
}
The assignments in Equipment's constructor are in the wrong order. It's not a good idea to use public fields either. Fields are implementation details, even if they are public.
The class should look like this:
public class Equipment
{
public string Name {get;set;}
public string Description {get;set;}
public string Type {get;set;}
public int Distance_moved {get;set;}= 0;
public int Wheels {get;set;} =0;
public int Weight {get;set;} =0;
public int Maintenance_cost {get;set;} =0;
public Equipment(string name, string description, string type,
int wheels, int weight)
{
Name = name;
Description = description;
Type = type;
Wheels = wheels;
Weight = weight;
}

Linking instances of two classes together

Programming newbie here and I've been breaking my head over this for several hours now.
I can make a coordinate object but then I want to make a dot object that can access the coordinate fields from a Coordinate object. How do I "link" these two classes together? And do you have any recommendations for good YouTube videos that explain what I'm missing here? Thanks!
class Coordinate
{
public int X { get; private set; } = 0;
public int Y { get; private set; } = 0;
public Coordinate(int x, int y)
{
x = X;
y = Y;
}
}
class Dot
{
public string color { get; set; }
public Dot(string color, Dot dot)
{
this.Color = color;
}
}
class Program
{
static void Main(string[] args)
{
Coordinate coor1 = new Coordinate(2, 3);
Dot dot1 = new Dot("Blue", coor1);
}
Here is what you are searching for a "linking" your classes. In object-oriented programming this is called composition.
That way you can use functionality and data of Coordinate-instance inside your Dot class.
class Coordinate
{
public int X { get; private set; }
public int Y { get; private set; }
public Coordinate(int x, int y)
{
X = x;
Y = y;
}
}
class Dot
{
public Coordinate coord { get; private set; }
public string color { get; set; }
public Dot(string color, Coordinate coord)
{
this.color = color;
this.coord = coord;
}
}
class Program
{
static void Main(string[] args)
{
Coordinate coor1 = new Coordinate(2, 3);
Dot dot1 = new Dot("Blue", coor1);
Console.WriteLine(dot1.coord.X);
}
}
Note: I also fixed possible typo in Coordinate-constructor (setting X=x and Y=y..)

How can I display as text the properties of an element in a textbox in windows form application c#?

I am writing a program that calculates keplerian elements. the main function gets 2 lines of tle, and return the object kep and his properties are the parameters of the sattelite orbit. I have to display these parameters in a textbox, but I dont understand how exactly am I supposed to do it.
any help will be appriciated :)
form1.cs:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void inputtext_TextChanged(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void retrieveInput_Click(object sender, EventArgs e)
{
String line1 = line1String.Text;
String line2 = line2String.Text;
//what happens after the recieving of the strings is the calculation and displating the kep properties in textbox "ShowBox"
}
private void textBox1_TextChanged_1(object sender, EventArgs e)
{
}
}
my kepelments class:
class KepElements
{
public double raan { get; set; }
public double argperi { get; set; }
public double meanan { get; set; }
public double meanmotion { get; set; }
public double eccentricity { get; set; }
public double bstar { get; set; }
public double epochYear { get; set; }
public double inclination { get; set; }
public double epochDay { get; set; }
}
and the main program:
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
//function that gets 2 strings and calculates all elemnts
public static KepElements calculating(String line1, String line2)
{
double raan, argperi, meanan, meanmotion, eccentricity, bstar, epochYear, inclination, SepochDay, sec, epochDay;
int CurrentSec, sec2, sec3;
KepElements kep = new KepElements();
//setting eccentricity
eccentricity = Convert.ToDouble(line2.Substring(28, 7));
kep.eccentricity = eccentricity;
//setting the bstar
bstar = Convert.ToDouble(line2.Substring(55, 8));
kep.bstar = bstar;
//setting the epochYear
epochYear = Convert.ToDouble(line2.Substring(20, 2));
kep.epochYear = epochYear;
//calculating the EpochDay
SepochDay = Convert.ToDouble(line2.Substring(22, 12));
sec = ((SepochDay - Math.Truncate(SepochDay)) * 100000000); //the time of seconds (after the decimal point)
sec3 = (int)sec;
sec2 = (int)SepochDay;
sec2 = sec2 * 86400;//sec2 is now the number of sec in a day*number of days since beggining of the current year
CurrentSec = (DateTime.Now.Year - 1970) * 31556926;
epochDay = CurrentSec + sec + sec2;
kep.epochDay = epochDay;
//calculating the inclination
inclination = Convert.ToDouble(line2.Substring(10, 8));
inclination = (inclination / 180) * Math.PI;
kep.inclination = inclination;
//calculating the meananomaly
meanan = Convert.ToDouble(line2.Substring(45, 8));
meanan = (meanan / 180) * Math.PI;
kep.meanan = meanan;
//calculating the argue of perigee
argperi = Convert.ToDouble(line2.Substring(36, 8));
argperi = (argperi / 180) * Math.PI;
kep.argperi = argperi;
//calculating the mean motion
meanmotion = Convert.ToDouble(line2.Substring(54, 11));
meanmotion = (meanmotion / 1440) * Math.PI * 2;
kep.meanmotion = meanmotion;
//calculating the raan
raan = Convert.ToDouble(line2.Substring(19, 8));
raan = (raan / 180) * Math.PI;
kep.raan = raan;
return kep;
}
}
You can either manually add all the values with labels using $"Label: {value}\r\n" or use a StringBuilder class. Another option is to use reflection on the class holding the data and build your text output by automatically looking at the properties and their values. Then just assign the built text string to the text box.
You could also use the string override ToString() in your KepElements class.
Requires:
using System.Reflection;
Example:
class KepElements
{
public double raan { get; set; }
public double argperi { get; set; }
public double meanan { get; set; }
[UserFriendlyName("Mean Motion")]
public double meanmotion { get; set; }
public double eccentricity { get; set; }
public double bstar { get; set; }
public double epochYear { get; set; }
public double inclination { get; set; }
public double epochDay { get; set; }
public override string ToString()
{
StringBuilder build = new StringBuilder();
foreach (var property in typeof(KepElements).GetProperties())
{
build.AppendLine($"{property.GetUserFriendlyName()}: {property.GetValue(this)}");
}
return build.ToString();
}
}
[AttributeUsage(AttributeTargets.Property)]
public class UserFriendlyName : Attribute
{
public UserFriendlyName(string name)
{
Name = name;
}
public string Name { get; private set; }
}
public static class GetUserFriendlyNameExt
{
public static string GetUserFriendlyName(this PropertyInfo obj)
{
object[] attribs = obj.GetCustomAttributes(typeof(UserFriendlyName),false);
if (attribs != null && attribs.Length > 0)
return (attribs[0] as UserFriendlyName)?.Name;
else
return obj.Name;
}
}

C# Poker How to check if players have different hands

I'm developing a Poker Texas Hold'Em app I have all the combinations working but i cant seem to find a compact way to check which player has the highest combination and the type of it here's some of my code
void Rules(int current,int Power)
{
//checking if the player has any pair
{
Power = 1;
current = 1;
}
}
current = the type of the hand (pair=1,two pair =2..straight flush=8)
Power = the power of the hand (pair of deuces=2....pair of kings=13)
and so on.. I'm updating current and Power with each new combination and since they are parameters of the void each player has his own "current" and "Power" so they don't get messed up.This is what i have so far and my question is how do i check which player has the highest hand without using 20-30 repetitive if statements i was thinking about :
List <int> Arr = new List <int>();
void Rules(int current,int Power)
{
//checking if the player has any pair
{
Power = 1;
current = 1;
Arr.Add(Power);
Arr.Add(current);
}
}
but like this i have no idea which type belongs to which player so it's not use i also tried with strings but than i wont be able to compare them that easily.What should i do ? What's the right approach ?
You might want to make a class for reach rule, to encapsulate the logic. Something like this:
public class Card
{
public string Name { get; private set; }
/* cards have a value of 2-14 */
public int Value { get; private set; }
}
public abstract class Rule()
{
public abstract string Name { get; }
/* hands are calculated in powers of 100, when the card value is added you will get something like 335 */
public abstract int Value { get; }
public abstract bool HasHand(IReadonlyList<Card> cards);
}
public class PairRule() : Rule
{
public override string Name
{
get { return "Pair"; }
}
public override int Value
{
get { return 100; }
}
public override bool HasHand(IReadonlyList<Card> cards)
{
/* implement rule here */
return Enumerable.Any(
from x in cards
group x by x.Value into g
where g.Count() == 2
select g
);
}
}
...
public class Player
{
public IReadonlyList<Card> Hand { get; private set; }
public int GetHandValue(IReadonlyList<Rule> rules)
{
/* get value of hand 100, 200, 300 etc. */
var handValue = Enumerable.Max(
from x in rules
where x.HasHand(Hand)
select x.Value
);
/* get value of cards */
var cardValue = Hand
.OrderByDescending(x => x.Value)
.Take(5)
.Sum();
return handValue + cardValue;
}
}
public class Pot
{
public int Value { get; private set; }
public IReadonlyList<Player> Players { get; private set; }
public IReadonlyList<Player> GetWinners(IReadonlyList<Rule> rules)
{
var playerHands = Enumerable.ToList(
from x in players
select new {
Player = x,
HandValue = x.GetHandValue(rules)
}
);
var maxHand = playerHands.Max(x => x.HandValue);
return Enumerable.ToList(
from x in playerHands
where x.HandValue == maxHand
select x.Player
);
}
}

Get the index in a multidimensional class

Sorry for the title i will put here an example of what i want to accomplish:
namespace mdclass
{
class pClass
{
static void Main()
{
tiles<int> tl = new tiles<int>();
tl[0].X = 0;
}
}
class tiles<T> : List<T>
{
public int X
{
//get/set the X-coordonate
}
public int Y
{
//get/set the Y-coordonate
}
}
}
how can i transfer the [0] from the tl[0] in the public int X and work with it?
Create a class for x and y coordinates:
public sealed class Point {
public int X { get; set; }
public int Y { get; set; }
}
Use the Point class to store the coordinates into the list:
public sealed class Program {
public static void Main() {
var tiles = new List<Point>();
tiles.Add(new Point { X = 5, Y = 10 });
tiles[0].X = 15;
}
}
Could you not just make tl public?
Then myInt = mypClass.tl[0].X
A data heirachy like this might work for you (no time to add actual code, sorry!). Then you could implement appropriate getters and setters.
Public class Grid {
List<Row>
}
Public class Row{
List<Point>
}
Public Class Point{
public int X { get; set; }
public int Y { get; set; }
}

Categories

Resources