User Tools

Site Tools


notes:csharp:snippets

Code Snippets

Rainbow colors:

class RainbowColor
{
    public byte R { get; set; }
    public byte G { get; set; }
    public byte B { get; set; }
 
    int rainbowIndex = 0;
    int rainbowMax = 500;
 
    public void NextColor()
    {
        R = Convert.ToByte(255.0 * (1.0 + Math.Sin(Math.PI * (float)rainbowIndex / 
                                                  (float)(rainbowMax - 1))) / 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++;
        if (rainbowIndex > rainbowMax) rainbowIndex = 0;
 
        //Debug.WriteLine("R:{0:F2} G:{1:F2} B:{2:F2}", r, g, b);
    }
}

Binary search:

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
}

Image scaling: the following pseudo-csharp code scales an input image to fit into a target bounding rectangle:

// 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
notes/csharp/snippets.txt · Last modified: 2017/03/13 by admin