Home > Software design >  C# - Math / Logic - Checking which Value is nearer
C# - Math / Logic - Checking which Value is nearer

Time:10-07

I know this looks like a lot of text but it might be actual fun for you to solve it :)? See it as a simple math problem.

I struggle with my following logic, I have it in my head and want to start coding it but I don't know how and where to start. I have a Basic Idea and I will try to explain the whole thing as simple as possible but I need a lil help with how to code it. Maybe someone can show me how to code it or give me an improvement idear?

First of all I have a total of 8 doubles which serve as coordinates in my program.

2 of them are the static "main coordinates" which I want to test around. As an example:

double xmain = 110.0
double ymain = 120.0

I also have 2 more input double coordinates which change dynamically depending on what you type. for example:

double x = 80.0
double y = 90.0

now i want to compare double x with double xmain to see how close x is to xmain, the same with y and ymain.

For that I could do something like that:

double percentx = (x / xmain) * 100.0; // x is ~72% of xmain
double percenty = (y / ymain) * 100.0; // y is ~75% of ymain

but you have to assume too that the x value can be negative or higher as xmain so doing that wouldn't be a good idea as I get results like 140% or -50%. So is there another approach I could try?

Second thing I want to do is after comparing how close these two are to xmain and ymain compare if x and y are closer to xmain and ymain than the following doubles:

double nearestx = 85.0
double nearesty = 75.0

If the sum of x and y is closer to xmain and x is closer to ymain than nearestx and nearest y, then x and y should overwrite the values of nearestx and nearest y with their own values.

I tried something like that using the

  • Related