Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.71 KB | None | 0 0
  1. private void AntyAliasedLineDraw(Point from, Point to, int grubosc)
  2.         {
  3.  
  4.             double offsetX = to.X - from.X;
  5.             double offsetY = to.Y - from.Y;
  6.             var fromX = from.X;
  7.             var fromY = from.Y;
  8.             var toX = to.X;
  9.             var toY = to.Y;
  10.  
  11.             if (Math.Abs(offsetX) >= Math.Abs(offsetY))
  12.             {
  13.                 // robimy po x
  14.                 if (from.X > to.X)
  15.                 {
  16.                     fromX = to.X;
  17.                     fromY = to.Y;
  18.                     toX = from.X;
  19.                     toY = from.Y;
  20.                     offsetX = -offsetX;
  21.                     offsetY = -offsetY;
  22.                 }
  23.  
  24.                 var yPerPixel = offsetY/offsetX;
  25.                 double actualHeight = _Sampling*fromY;
  26.  
  27.                 for (var j = _Sampling * fromX; j <= _Sampling * toX; j++) // robimy po X-ach
  28.                 {
  29.                     var bottom = (int)Math.Floor((double)actualHeight + grubosc*_Sampling);
  30.                     var top = (int)Math.Ceiling((double) actualHeight);
  31.  
  32.                     for (var i = top; i <= bottom; i++)
  33.                         _Pkts[j,i] = true;
  34.  
  35.                     actualHeight += yPerPixel;
  36.                 }
  37.  
  38.                 actualHeight = fromY;
  39.  
  40.                 for (var j = fromX; j <= toX; j++)
  41.                 {
  42.                     var bottom = (int)Math.Floor((double)actualHeight + grubosc);
  43.                     var top = (int)Math.Ceiling((double)actualHeight);
  44.  
  45.                     for (var i = top; i <= bottom; i++ )
  46.                     {
  47.                         var ile = 0;
  48.                         for (var k = 0; k < _Sampling; k++)   // x
  49.                             for (var l = 0 ; l < _Sampling; l++) // y
  50.                             {
  51.                                 if (_Pkts[_Sampling * j + k, _Sampling * i + l] == true)
  52.                                 {
  53.                                     _Pkts[_Sampling*j + k, _Sampling*i + l] = false;
  54.                                     ++ile;
  55.                                 }
  56.                             }
  57.  
  58.                         var stos = (double) ile/(double) (_Sampling*_Sampling);
  59.                         stos = 1 - stos;
  60.                         //var stos = 0;
  61.                         var war = (int)Math.Floor(255.00*stos);
  62.                         _Bmp.SetPixel(j, i, Color.FromArgb(war, war, war));
  63.                     }
  64.                         actualHeight += yPerPixel;
  65.                 }
  66.  
  67.  
  68.             }
  69.             else
  70.             {
  71.                 // robimy po y
  72.                 if (from.Y > to.Y)
  73.                 {
  74.                     fromX = to.X;
  75.                     fromY = to.Y;
  76.                     toX = from.X;
  77.                     toY = from.Y;
  78.                     offsetX = -offsetX;
  79.                     offsetY = -offsetY;
  80.                 }
  81.  
  82.                 var xPerPixel = offsetX / offsetY;
  83.                 double actualWidth = _Sampling * fromX;
  84.  
  85.                 for (var i = _Sampling * fromY; i <= _Sampling * toY; i++) // robimy po X-ach
  86.                 {
  87.                     var right = (int)Math.Floor((double)actualWidth + grubosc * _Sampling);
  88.                     var left = (int)Math.Ceiling((double)actualWidth);
  89.  
  90.                     for (var j = left; j <= right; j++)
  91.                         _Pkts[j, i] = true;
  92.  
  93.                     actualWidth += xPerPixel;
  94.                 }
  95.  
  96.                 actualWidth = fromX;
  97.  
  98.                 for (var i = fromY; i <= toY; i++)
  99.                 {
  100.                     var right = (int)Math.Floor((double)actualWidth + grubosc);
  101.                     var left = (int)Math.Ceiling((double)actualWidth);
  102.  
  103.                     for (var j = left; j <= right; j++)
  104.                     {
  105.                         var ile = 0;
  106.                         for (var l = 0; l < _Sampling; l++)   // y
  107.                             for (var k = 0; k < _Sampling; k++) // x
  108.                             {
  109.                                 if (_Pkts[_Sampling * j + k, _Sampling * i + l] == true)
  110.                                 {
  111.                                     _Pkts[_Sampling*j + k, _Sampling*i + l] = false;
  112.                                     ++ile;
  113.                                 }
  114.                             }
  115.  
  116.                         var stos = (double)ile / (double)(_Sampling * _Sampling);
  117.                         stos = 1 - stos;
  118.                         //var stos = 0;
  119.                         var war = (int)Math.Floor(255.00 * stos);
  120.                         _Bmp.SetPixel(j, i, Color.FromArgb(war, war, war));
  121.                     }
  122.                     actualWidth += xPerPixel;
  123.                 }
  124.  
  125.             }
  126.  
  127.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement