Need C# snippet

myndeswx

Client
Регистрация
15.05.2017
Сообщения
436
Благодарностей
104
Баллы
43
Looking for a smart C # programmer, need to work with some math logic.
Here's what need to be done -

We have a list of coordinates


1. 54.91793; 23.92322
2. 54.91794; 23.92291
3. 54.91796; 23.92244
It would look something like this if we draw a route on the map -
87250


Sometimes coordinates are close to each other, but sometimes they are far away, for example we take line 1 and line 2
54.917 93 54.917 94 is only incremented by 0.00001 - that is good (distance is close) BUT, 23.92 322 and 23.92 291 has decreased by 31 (difference can be positive or negative because of how coordinates work) and that is too much, we would look for a limit of 0.00020.
The script should generate the missing coordinates in

1.54.91793; 23.92322
2.54.91793; 23.92300 - auto generated
3.54.91794; 23.92291
4.54.91795; 23.92275 - auto generated
5.54.91796; 23.92244

Hope it's understandable, if you think you can do this please write here or telegram - @mindeswx : az:
 

BAZAg

Client
Регистрация
08.11.2015
Сообщения
1 788
Благодарностей
2 453
Баллы
113
Looking for a smart C # programmer, need to work with some math logic.
Here's what need to be done -

We have a list of coordinates


1. 54.91793; 23.92322
2. 54.91794; 23.92291
3. 54.91796; 23.92244
It would look something like this if we draw a route on the map -
Посмотреть вложение 87250

Sometimes coordinates are close to each other, but sometimes they are far away, for example we take line 1 and line 2
54.917 93 54.917 94 is only incremented by 0.00001 - that is good (distance is close) BUT, 23.92 322 and 23.92 291 has decreased by 31 (difference can be positive or negative because of how coordinates work) and that is too much, we would look for a limit of 0.00020.
The script should generate the missing coordinates in

1.54.91793; 23.92322
2.54.91793; 23.92300 - auto generated
3.54.91794; 23.92291
4.54.91795; 23.92275 - auto generated
5.54.91796; 23.92244

Hope it's understandable, if you think you can do this please write here or telegram - @mindeswx : az:
C#:
System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("en-EN");

List<string> list = new List<string>();
list.Add("54.91793; 23.92322");
list.Add("54.91794; 23.92291");
list.Add("54.91796; 23.92244");

PointF limit = new PointF(
    float.Parse("0.0002",System.Globalization.NumberStyles.Float, ci),
    float.Parse("0.0002",System.Globalization.NumberStyles.Float, ci));

List<PointF> points = new List<PointF>();
foreach(string line in list) {
    float x = float.Parse(line.Split(';')[0].Trim(),System.Globalization.NumberStyles.Float, ci);
    float y = float.Parse(line.Split(';')[1].Trim(),System.Globalization.NumberStyles.Float, ci);
    PointF p = new PointF((float)Math.Round(x,5),(float)Math.Round(y,5));
    points.Add(p);
}

List<PointF> pointresult = new List<PointF>();

for(int i=0; i<points.Count; i++) {
    PointF a = points[i]; // start
    pointresult.Add(a);
    if( i + 1 >= points.Count) break;
   
    PointF b = points[i+1]; // end
   
    int count_x = (int)Math.Truncate(Math.Abs(b.X - a.X) / limit.X);
    project.SendInfoToLog("count x: " +count_x.ToString());
    int count_y = (int)Math.Truncate(Math.Abs(b.Y - a.Y) / limit.Y);
    project.SendInfoToLog("count y: " +count_y.ToString());
       
    if(count_x == 0 && count_y == 0 ) break;
       
    float axx = a.Y - b.Y;
    float byy = b.X - a.X;
    float c = a.X*b.Y-b.X*a.Y;

    if(count_x == 0 && count_y > 0) {
        float[] new_points_y = Enumerable.Range(1, count_y).Select(m => (float)Math.Round((a.Y + (m * limit.Y)),5)).ToArray();
        for(int yy = 0;yy < new_points_y.Length;yy++) {          
            float newx = (float)Math.Round((-byy*new_points_y[yy]-c)/axx,5);
            if(!(newx >= a.X && newx<=b.X)) newx = (float)Math.Round(((a.X-b.X)/new_points_y.Length) * yy+a.X, 5);
            pointresult.Add(new PointF(  newx , new_points_y[yy]));
        }
    }
    else if(count_x > 0 && count_y == 0) {
        float[] new_points_x = Enumerable.Range(1, count_x).Select(m => (float)Math.Round((a.X + (m * limit.X)),5)).ToArray();
        for(int xx = 0;xx < new_points_x.Length;xx++) {      
            float newy = (float)Math.Round((-axx*new_points_x[xx]-c)/byy,5);
            if(!(newy >= a.Y && newy<=b.Y))  newy = (float)Math.Round(((a.Y-b.Y)/new_points_x.Length) * xx+a.Y, 5);              
            pointresult.Add(new PointF(new_points_x[xx], newy));
        }  
    }
    else if(count_x > count_y){
        float[] new_points_x = Enumerable.Range(1, count_x).Select(m => (float)Math.Round((a.X + (m * limit.X)),5)).ToArray();
        for(int xx = 0;xx < new_points_x.Length;xx++) {          
            float newy = (float)Math.Round((-axx*new_points_x[xx]-c)/byy,5);
            if(!(newy >= a.Y && newy<=b.Y))  newy = (float)Math.Round(((a.Y-b.Y)/new_points_x.Length) * xx+a.Y, 5);              
            pointresult.Add(new PointF(new_points_x[xx], newy));
        }
    }
    else {  
        float[] new_points_y = Enumerable.Range(1, count_y).Select(m => (float)Math.Round((a.Y + (m * limit.Y)),5)).ToArray();
        for(int yy = 0; yy < new_points_y.Length; yy++) {  
            float newx = (float)Math.Round((-byy*new_points_y[yy]-c)/axx,5);
            if(!(newx >= a.X && newx<=b.X))  newx = (float)Math.Round(((a.X-b.X)/new_points_y.Length) * yy+a.X, 5);
            pointresult.Add(new PointF(  newx , new_points_y[yy]));
        }  
    }
}


List<string> list_out = pointresult.Select(p => string.Format("{0}; {1}", p.X, p.Y).Replace(",",".")).ToList();
return string.Join(Environment.NewLine, list_out);
87271


Fixed: it is necessary to replace a line in the code, otherwise the loop may be interrupted prematurely.
if(count_x == 0 && count_y == 0 ) continue;
 
Последнее редактирование:
  • Спасибо
Реакции: myndeswx

myndeswx

Client
Регистрация
15.05.2017
Сообщения
436
Благодарностей
104
Баллы
43
Working like a charm, thank you!!!
Your link in signature is not working :az:
 

BAZAg

Client
Регистрация
08.11.2015
Сообщения
1 788
Благодарностей
2 453
Баллы
113
  • Спасибо
Реакции: myndeswx

myndeswx

Client
Регистрация
15.05.2017
Сообщения
436
Благодарностей
104
Баллы
43
Turns out there is some issue, sometimes more coordinates are added, and sometimes not enough: al:
Much easier to see it visually
The the the Input of The Is the list of the the this coordinates-
This is the coordinates for the this Map-

C#:
54.92173; 23.93389
54.92174; 23.93394
54.92177; 23.93399
54.92184; 23.93405
54.92204; 23.93414
54.92204; 23.93414
54.9221; 23.93381
54.9222; 23.93321
54.9222; 23.93321
54.92268; 23.93348
54.92314; 23.93371
54.92314; 23.93371
54.92335; 23.93253
54.9236; 23.93071
54.92391; 23.92853
54.92391; 23.92853
54.92373; 23.9285
54.92241; 23.92831
54.92212; 23.92826
54.92179; 23.9282
54.92093; 23.92811
87294



And the this is what the script generates-
87295

For some large Have for We the GAPS, and sometimes Do the Do 'drift' off course the the


This is the Desired result-
87296


If you or someone else would like to work to finish it please let me know, willing to pay of course!
 

Кто просматривает тему: (Всего: 1, Пользователи: 0, Гости: 1)