Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Sortowanie w MySQL, 3 warunki
--michu--
post
Post #1





Goście







Witam,
mam tabele z kolorami kolorami w RGB (kolumna R, kolumna G, kolumna (IMG:http://forum.php.pl/style_emoticons/default/cool.gif) , chce je wyświetlić tak żeby kolor kolejnych rekordów przechodził płynnie. Czy jest sposób zeby zrobic sortowanie ktre warunkuja te 3 pola?
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
dr_bonzo
post
Post #2





Grupa: Przyjaciele php.pl
Postów: 5 724
Pomógł: 259
Dołączył: 13.04.2004
Skąd: N/A

Ostrzeżenie: (0%)
-----


erix: ani troche

  1. <?php
  2. // sortuje kolory wg podobienstwa
  3. $colors = generateColors( 100 );
  4. $colorsx = array(
  5. array( 0, 0, 0 ),
  6. array( 20, 20, 20 ),
  7. array( 30, 30, 30 ),
  8. array( 40, 40, 40 ),
  9. array( 50, 50, 50 ),
  10. array( 60, 60, 60 ),
  11. array( 0, 0, 50 )
  12. );
  13. $sortedColors = sortColors( $colors );
  14. printColors( $sortedColors );
  15.  
  16. function generateColors( $n )
  17. {
  18. $min = 0;
  19. $max = 255;
  20. $colors = array();
  21. for ( $i = 0; $i < $n; $i++ )
  22. {
  23. $color = array();
  24. $color[] = rand( $min, $max );
  25. $color[] = rand( $min, $max );
  26. $color[] = rand( $min, $max );
  27.  
  28. $colors[] = $color;
  29. }
  30. return $colors;
  31. }
  32.  
  33. function sortColors( $colors )
  34. {
  35. $sorted = array();
  36. $sorted[] = array_pop( $colors );
  37. while ( ! empty( $colors ) )
  38. {
  39. // last color
  40. $last = end( $sorted );
  41.  
  42. // find closest one
  43. $nextIndex = findClosest( $colors, $last );
  44. $next = $colors[ $nextIndex ];
  45. // remove it from colors
  46. unset( $colors[ $nextIndex ] );
  47.  
  48. // add to sorted colors
  49. $sorted[] = $next;
  50. }
  51. return $sorted;
  52. }
  53.  
  54. function findClosest( $colors, $c )
  55. {
  56. // first as best
  57. $best = reset( $colors );
  58. $bestIndex = key( $colors );
  59. $bestDifference = calculateDifference( $c, $best );
  60. // search better
  61. foreach ( $colors as $i => $v )
  62. {
  63. $newDiff = calculateDifference( $c, $v );
  64. if ( $newDiff < $bestDifference )
  65. {
  66. $best = $v;
  67. $bestIndex = $i;
  68. $bestDifference = $newDiff;
  69. }
  70. }
  71. return $bestIndex;
  72. }
  73.  
  74.  
  75. function calculateDifference( $from, $to )
  76. {
  77. $sum = 0;
  78. $pow = 10;
  79. $sum += pow( $from[0] - $to[0], $pow );
  80. $sum += pow( $from[1] - $to[1], $pow );
  81. $sum += pow( $from[2] - $to[2], $pow );
  82. return $sum;
  83. }
  84. function printColors( $colors )
  85. {
  86. foreach ( $colors as $color )
  87. {
  88. printf( '<div style="background-color: rgb( %d, %d, %d ); width: 200px; height: 5px;"></div>', $color[0], $color[1], $color[2] );
  89. }
  90. }
  91.  
  92. ?>


co wy na to? (IMG:http://forum.php.pl/style_emoticons/default/smile.gif)
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 13.10.2025 - 21:00