User Tools

Site Tools


notes:csharp:snippets

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
notes:csharp:snippets [2016/01/14]
admin created
notes:csharp:snippets [2020/08/26] (current)
Line 1: Line 1:
 ====== Code Snippets ====== ====== Code Snippets ======
  
-**Snippet**: Rainbow colors: +Rainbow colors: 
 <code csharp> <code csharp>
-class ColorRainbow+class RainbowColor
 { {
     public byte R { get; set; }     public byte R { get; set; }
Line 14: Line 14:
     public void NextColor()     public void NextColor()
     {     {
-        R = Convert.ToByte(255.0 * (1.0 + Math.Sin(Math.PI * (float)rainbowIndex / (float)(rainbowMax - 1))) / 2.0); +        R = Convert.ToByte(255.0 * (1.0 + Math.Sin(Math.PI * (float)rainbowIndex /  
-        G = Convert.ToByte(255.0 * (1.0 + Math.Sin(Math.PI * (float)rainbowIndex / (float)(rainbowMax - 1) + 2.0 * Math.PI / 3.0)) / 2.0); +                                                  (float)(rainbowMax - 1))) / 2.0); 
-        B = Convert.ToByte(255.0 * (1.0 + Math.Sin(Math.PI * (float)rainbowIndex / (float)(rainbowMax - 1) + 4.0 * Math.PI / 3.0)) / 2.0);+        G = Convert.ToByte(255.0 * (1.0 + Math.Sin(Math.PI * (float)rainbowIndex /  
 +                                                  (float)(rainbowMax - 1) + 2.0 * Math.PI / 3.0)) / 2.0); 
 +        B = Convert.ToByte(255.0 * (1.0 + Math.Sin(Math.PI * (float)rainbowIndex /  
 +                                                  (float)(rainbowMax - 1) + 4.0 * Math.PI / 3.0)) / 2.0);
         rainbowIndex++;         rainbowIndex++;
         if (rainbowIndex > rainbowMax) rainbowIndex = 0;         if (rainbowIndex > rainbowMax) rainbowIndex = 0;
Line 23: Line 26:
     }     }
 } }
 +</code>
 +
 +
 +Binary search:
 +<code csharp>
 +public int BinarySearch<T>(T[] arr, T val) where T : IComparable<T>
 +{
 +    int low = 0;                // low subscript
 +    int high = arr.Length - 1;  // high subscript
 +    int middle;                 // middle subscript
 +
 +    while (low <= high)
 +    {
 +        middle = (low + high) / 2;
 +
 +        if (val.Equals(arr[middle]))                // match
 +            return middle;                          // found; return the index
 +        else if (val.CompareTo(arr[middle]) == -1)  // val < arr[middle]
 +            high = middle - 1;                      // search low end of array
 +        else
 +            low = middle + 1;
 +    }
 +
 +    return -1; // val not found
 +}
 +</code>
 +
 +
 +Image scaling: the following pseudo-csharp code scales an input image to fit into a target bounding rectangle:
 +<code csharp>
 +// Scale an image into a bounding rectangle specified by:
 +// - left-upper corner (x1,y1)
 +// - right-bottom corner (x2,y2)
 +int x1, y1, x2, y2; 
 +
 +// Input image size.
 +float img_Height, img_Width;
 +
 +// Calculate the size of the bounding rectangle.
 +// These variables will be adjusted to reflect the target size 
 +// of the image.
 +float w = x2 - x1;
 +float h = y2 - y1;
 +
 +// Calculate helper ratios.
 +float img_hw = img_Height / img_Width;
 +float img_wh = img_Width / img_Height;
 +
 +// Both the width and the height of the image are outside or inside the bounding rectangle.
 +if (img_Width < w && img_Height < h // within the bounding rectangle
 +    || 
 +    img_Width > w && img_Height > h) // outside of the bounding rectangle
 +{
 +    // Scale the width or the height of the image.
 +
 +    // Ratios image-to-boundingRectangle
 +    float rw = img_Width / w;
 +    float rh = img_Height / h;
 +
 +    // Scale accordingly.
 +    if (rw > rh)
 +        h = w * img_hw;
 +    else
 +        w = h * img_wh;
 +}
 +else if (img_Height > h && img_Width < w) // height outside of bounding rectangle
 +{
 +    w = h * img_wh;
 +}
 +else if (img_Width > w && img_Height < h) // width outside of bounding rectangle
 +{
 +    h = w * img_hw;
 +}
 +
 +// Output size of the image: w and h
 </code> </code>
  
  
notes/csharp/snippets.1452783365.txt.gz · Last modified: 2020/08/26 (external edit)