/* This draws a triangle with points (x0, y0), (x1, y1), (x2, y2) in the colour * defined by (r, g, b). It uses the computation of barycentric coordinates * to check whether each point is inside the triangle. It only checks points * that are inside the bounding box of the triangle. */ void draw_triangle(float x0, float y0, float x1, float y1, float x2, float y2, byte r, byte g, byte b) { /* Finds the bounding box of the triangle */ int xmin = (int) floor(min_3(x0, x1, x2)); int xmax = (int) ceil(max_3(x0, x1, x2)); int ymin = (int) floor(min_3(y0, y1, y2)); int ymax = (int) ceil(max_3(y0, y1, y2)); float alpha, beta, gamma; /* Equations for the alpha, beta and gamma points in respect to the lines * of the triangle */ float alpha_function = f(x1, x2, y1, y2, x0, y0); float beta_function = f(x2, x0, y2, y0, x1, y1); float gamma_function = f(x0, x1, y0, y1, x2, y2); /* Equations for the offscreen points in respect to the * lines of the triangle */ float alpha_function_offscreen = f(x1, x2, y1, y2, OFFSCREEN[0], OFFSCREEN[1]); float beta_function_offscreen = f(x2, x0, y2, y0, OFFSCREEN[0], OFFSCREEN[1]); float gamma_function_offscreen = f(x0, x1, y0, y1, OFFSCREEN[0], OFFSCREEN[1]); for(int y = ymin; y <= ymax; y++) { for(int x = xmin; x <= xmax; x++) { /* Computes the new alpha, beta and gamma values and divides by * The function value of the corresponding lines to get a value * between 0 and 1 when it is inside the triangle, below zero * when not. */ alpha = f(x1, x2, y1, y2, x, y) / alpha_function; beta = f(x2, x0, y2, y0, x, y) / beta_function; gamma = f(x0, x1, y0, y1, x, y) / gamma_function; if(alpha >= 0 && beta >= 0 && gamma >= 0) { /* Only draw if it's inside the triangle, or on the triangle edge * if the triangle is on the side of the arbitrarily chosen * offscreen point. This prevents shared edges being drawn twice */ if((alpha > 0 || alpha_function * alpha_function_offscreen > 0) && (beta > 0 || beta_function * beta_function_offscreen > 0) && (gamma > 0 || gamma_function * gamma_function_offscreen > 0)) { PutPixel(x, y, r, g, b); } } } } }
int getUglyNumber(int index) { if (index <= 0) { return 0; } int* ugly = new int[index]; ugly[0] = 1; int index2 = 0; int index3 = 0; int index5 = 0; for (int i = 1;i<index;i++) { ugly[i] = min_3(ugly[index2] * 2, ugly[index3] * 3, ugly[index5] * 5); while (ugly[i] >= ugly[index2] * 2) { index2++; } while (ugly[i] >= ugly[index3] * 3) { index3++; } while (ugly[i] >= ugly[index5] * 5) { index5++; } } int ans = ugly[index - 1]; delete[] ugly; return ans; }
int minDistance(char* word1, char* word2) { int **dp; int len1 = strlen(word1); int len2 = strlen(word2); int i,j; int ret; dp = (int **)malloc(sizeof(int *) * (len1 + 1)); for(i = 0; i <= len1; ++i){ dp[i] = (int *)malloc(sizeof(int) * (len2 + 1)); } for(i = 0; i <= len1; ++i) dp[i][0] = i; for(j = 0; j <= len2; ++j) dp[0][j] = j; for(i = 1; i <= len1; ++i){ for(j = 1; j <= len2; ++j){ dp[i][j] = min_3(dp[i-1][j] + 1, dp[i][j-1] + 1, dp[i-1][j-1] + (word1[i-1] == word2[j-1] ? 0 : 1)); } } ret = dp[len1][len2]; for(i = 0; i <= len1; ++i) free(dp[i]); free(dp); return ret; }
int main() { int num = 0, p2, p3, p5, n; long a[1501]; a[1] = p2 = p3 = p5 = n = 1; while(1) { a[++ n] = min_3(2 * a[p2], 3 * a[p3], 5 * a[p5]); if(a[n] == 2 * a[p2]) p2++; if(a[n] == 3 * a[p3]) p3++; if(a[n] == 5 * a[p5]) p5++; if(n == 1500) break; } printf("The 1500'th ugly number is %ld.\n", a[n]); return 0; }
long long get_ham(int p1, int p2, int p3, int index) { long long i = 1, i1 = 0, i2 = 0, i3 = 0; hams[0] = 1; while (i <= index) { hams[i] = min_3(hams[i1]*p1, hams[i2]*p2, hams[i3]*p3); if (hams[i] == hams[i1]*p1) i1++; if (hams[i] == hams[i2]*p2) i2++; if (hams[i] == hams[i3]*p3) i3++; i++; } return hams[index]; }
/* An optimised version of the draw_triangle function. It uses an incremental * approach to computing the barycentric coordinates, only computing a start * value and then incrementing that value every step along the way, using * f_incr. It also gets rid of all floating point operations, favouring * integers instead. This means that the alpha, beta and gamma values are not * between 0 and 1 when a point is within the triangle, but instead it is just * greater than or equal to 0, so it can still be tested equally well. * If you were to use it for interpolation (eg. of colours) however, you would * run into problems. */ void draw_triangle_optimized(float x0, float y0, float x1, float y1, float x2, float y2, byte r, byte g, byte b) { /* Finds the bounding box of the triangle */ int xmin = (int) floor(min_3(x0, x1, x2)); int xmax = (int) ceil(max_3(x0, x1, x2)); int ymin = (int) floor(min_3(y0, y1, y2)); int ymax = (int) ceil(max_3(y0, y1, y2)); /* Finds the sign of all points in respect to the lines */ int alpha_sign = 1; int beta_sign = 1; int gamma_sign = 1; if(signbit(f(x1, x2, y1, y2, x0, y0))) alpha_sign = -1; if(signbit(f(x2, x0, y2, y0, x1, y1))) beta_sign = -1; if(signbit(f(x0, x1, y0, y1, x2, y2))) gamma_sign = -1; /* Finds the sign of the offscreen point in respect to the lines */ int alpha_sign_offscreen = 1; int beta_sign_offscreen = 1; int gamma_sign_offscreen = 1; if(signbit(f(x1, x2, y1, y2, OFFSCREEN[0], OFFSCREEN[1]))) alpha_sign_offscreen = -1; if(signbit(f(x2, x0, y2, y0, OFFSCREEN[0], OFFSCREEN[1]))) beta_sign_offscreen = -1; if(signbit(f(x0, x1, y0, y1, OFFSCREEN[0], OFFSCREEN[1]))) gamma_sign_offscreen = -1; /* Computes the starting values for alph, beta and gamma, for the point * (xmin - 1, ymin - 1) so that it will be incremented to the correct * starting value in the first loop cycle. */ int alpha = f(x1, x2, y1, y2, xmin - 1, ymin - 1) * alpha_sign; int beta = f(x2, x0, y2, y0, xmin - 1, ymin - 1) * beta_sign; int gamma = f(x0, x1, y0, y1, xmin - 1, ymin - 1) * gamma_sign; for(int y = ymin; y <= ymax; y++) { alpha += f_incr(x1, x2, 0, 0, alpha_sign); beta += f_incr(x2, x0, 0, 0, beta_sign); gamma += f_incr(x0, x1, 0, 0, gamma_sign); /* Saves the current values to jump back to after the * x loop is finished */ int prev_alpha = alpha; int prev_beta = beta; int prev_gamma = gamma; for(int x = xmin; x <= xmax; x++) { alpha += f_incr(0, 0, y1, y2, alpha_sign); beta += f_incr(0, 0, y2, y0, beta_sign); gamma += f_incr(0, 0, y0, y1, gamma_sign); if(alpha >= 0 && beta >= 0 && gamma >= 0) { /* Only draw if it's inside the triangle, or on the triangle edge * if the triangle is on the side of the arbitrarily chosen * offscreen point. This prevents shared edges being drawn twice */ if((alpha > 0 || alpha_sign * alpha_sign_offscreen > 0) && (beta > 0 || beta_sign * beta_sign_offscreen > 0) && (gamma > 0 || gamma_sign * gamma_sign_offscreen > 0)) { PutPixel(x, y, r, g, b); } } } alpha = prev_alpha; beta = prev_beta; gamma = prev_gamma; } }