Nr kratki do pozycji XY

0

Witam
Mam problem, muszę wyznaczyć pozycje XY dla kolumny z liczby, liczby dodają się od 0 do 165 a wielkość grida do 15x11
Napisałem nie do końca taka procedurę, na pewno by działała, ale jest za długa i brzydka.

US nr kratki
if (US >=  16) and (US <=  30)  then begin
result.X := (US) - 15;
result.Y := 2;
end;
if (US >=  31) and (US <=  45)  then begin
result.X := (US) - 30;
result.Y := 3;
end;
if (US >=  46) and (US <=  60)  then begin
result.X := (US) - 45;
result.Y := 4;
end;
itd.
 

Też próbowałem (dla grida 5x5 ) z :

if US <= 5 then begin
result.X := US;
result.Y := 1;
end;
if US >= 6 then begin
result.X := US mod 5;
result.Y := US div 4;
end;
if (US = 10) or (US = 15) or (US = 20) or (US = 25)  then
result.X := 5 ;
if (US = 6) or (US = 11) Then
result.Y := result.Y + 1;
if (US = 20) or (US = 25) Then
result.Y := result.Y - 1;
end;
 

Ale też nie jest za dobrze

Jest jakiś algorytm żeby szybko i dobrze to obliczyć?
Dzięki.

0

E...Chodzi o takie coś?

Function GetPosition(Nr, Width, Height: Integer): TPoint;
Begin
 Result.X := Nr Mod Width;
 Result.Y := Nr Div Height;
End;
0

To samo wykorzystałem w mojej 2 procedurze ale dodałem warunek

if (US = 10) or (US = 15) or (US = 20) or (US = 25) 
result.X := 5 ;

bo bez tego X = 0

edit@
dobra poradziłem sobie z tym na razie

Function GetPosition(Nr, Width, Height: Integer): TPoint;
Begin
 Result.X := Nr Mod Width;
 Result.Y := Nr Div Height;
 if Result.X = 0 then
 result.X := Width;
 if Nr <= 30 Then
 result.Y := result.Y
 else
 result.Y := result.Y - 1;
End;

close

0

numeracja od 0 jest wygodniejsza, właśnie ze względu na to że nie trzeba później tak kombinować

1 użytkowników online, w tym zalogowanych: 0, gości: 1