package csa.tiletools.worldgen.mapgen.miny.render;

import csa.tiletools.worldgen.mapgen.processing.CSAProcessing;

public class RenderUtils
{
  public static int blend(int paramInt1, int paramInt2)
  {
    int i3 = paramInt2 >> 24 & 0xFF;
    int i2 = 255 - i3;
    int i = paramInt1 >> 16 & 0xFF;
    int j = paramInt2 >> 16 & 0xFF;
    int k = paramInt1 >> 8 & 0xFF;
    int m = paramInt2 >> 8 & 0xFF;
    int n = paramInt1 & 0xFF;
    int i1 = paramInt2 & 0xFF;
    int i4 = (i * i2 + j * i3) / 255;
    int i5 = (k * i2 + m * i3) / 255;
    int i6 = (n * i2 + i1 * i3) / 255;
    return -16777216 + (i4 << 16) + (i5 << 8) + i6;
  }

  public static int modH(int paramInt)
  {
    while (paramInt < 0) {
          paramInt += 360;
      }
    return paramInt % 360;
  }

  public static int multAlpha(int paramInt, float paramFloat)
  {
    int i = paramInt >> 24 & 0xFF;
    i = CSAProcessing.floor(i * paramFloat);
    return (paramInt & 0xFFFFFF) + (i << 24);
  }

  public static float smoothstep(float paramFloat1, float paramFloat2, float paramFloat3)
  {
    if (paramFloat1 < paramFloat2)
      return 0.0F;
    if (paramFloat1 >= paramFloat3)
      return 1.0F;
    paramFloat1 = (paramFloat1 - paramFloat2) / (paramFloat3 - paramFloat2);
    return paramFloat1 * paramFloat1 * (3.0F - 2.0F * paramFloat1);
  }

  public static float distanceToLine(float paramFloat1, float paramFloat2, float paramFloat3, float paramFloat4, float paramFloat5, float paramFloat6)
  {
    float f1 = paramFloat5 - paramFloat3;
    float f2 = paramFloat6 - paramFloat4;
    float f3 = paramFloat1 - paramFloat3;
    float f4 = paramFloat2 - paramFloat4;
    float f5 = (f1 * f3 + f2 * f4) / (f1 * f1 + f2 * f2);
    f5 = CSAProcessing.constrain(f5, 0.0F, 1.0F);
    float f6 = paramFloat3 + f1 * f5;
    float f7 = paramFloat4 + f2 * f5;
    return CSAProcessing.dist(paramFloat1, paramFloat2, f6, f7);
  }

  // see http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
  public static boolean pointInPolygon(float testx, float testy, Float[] vertx, Float[] verty)
  {
    int nvert = CSAProcessing.min(vertx.length, verty.length);
    boolean c = false;
    int i = 0;
    for (int j = nvert - 1; i < nvert; j = i++)
    {
        if ( ((verty[i]>testy) != (verty[j]>testy)) &&
                (testx < (vertx[j]-vertx[i]) * (testy-verty[i]) / (verty[j]-verty[i]) + vertx[i]) )
              c = !c;
    }
    return c;
  }
}

