Exemplo n.º 1
0
int main (void){
    void scalarMultiply(int nRows, int nCols, int matrix[nRows][nCols], int scalar);
    void displayMatrix(int nRows, int nCols, int matrix[nRows][nCols]);
    int sample_matrix[3][6] =
    {
        {7, 16, 55, 13, 12, 4},
        {12, 10, 52, 0, 7, 77},
        {-2, 1, 2, 4, 9, 63}
    };
    
    printf("Original matrix:\n");
    displayMatrix(3, 6, sample_matrix);
    
    scalarMultiply(3, 6, sample_matrix, 2);
    
    printf("\nMultiplied by 2:\n");
    displayMatrix(3, 6, sample_matrix);
    
    scalarMultiply(3, 6, sample_matrix, -1);
    
    printf("\nMultiplied by -1:\n");
    displayMatrix(3, 6, sample_matrix);
    
    return 0;
}
int main(void)
{
	void scalarMultiply(int matrix[3][5], int scalar);
	void displayMatrix(int matrix[3][5]);
	
	int sampleMatrix[3][5] = 
		{
			{ 7, 16, 55, 13, 12},
			{12, 10, 52,  0,  7},
			{-2,  1,  2,  4,  9}
		};

	printf("Original matrix:\n");
	displayMatrix(sampleMatrix);

	scalarMultiply(sampleMatrix, 2);
	printf("\nMultiplied by 2:\n");
	displayMatrix(sampleMatrix);

	scalarMultiply(sampleMatrix, -1);
	printf("\nThen multiplied by -1:\n");
	displayMatrix(sampleMatrix);

	return 0;
}
Exemplo n.º 3
0
//------------------------------------------------------------------------------
void calc_specular(GzRender *render, GzCoord N_orig, GzColor col, bool mulByK) {
    // N is already sent here after transformation
    GzCoord N = {0.0f,0.0f,0.0f};
    normalizeVector(N_orig, N);
    
    GzCoord AccumulatedSpecResult = {0.0f, 0.0f, 0.0f};
 
    for(int i = 0; i < render->numlights; i++)
    {
        float (*ls)[3] = static_cast<float (*)[3]>(render->lights[i]);
        GzCoord ls_L_orig = {ls[0][0], ls[0][1], ls[0][2]};       
        GzCoord E = {0, 0, -1};        
        
        GzCoord ls_L = {0.0f,0.0f,0.0f};
        normalizeVector(ls_L_orig, ls_L);   
        


        if (!shouldCalcLight(N, ls_L, E, N))
            continue;
        
        
        
        float N_dot_L = vectorDotProduct(N, ls_L);
        GzCoord left = {0.0f,0.0f,0.0f};
        scalarMultiply(N, N_dot_L * 2.0f, left);
        GzCoord R_orig = {0.0f,0.0f,0.0f};
        subtractVector(left, ls_L, R_orig);
        GzCoord R = {0.0f,0.0f,0.0f};
        normalizeVector(R_orig, R);
        
        GzCoord ls_intensity = {ls[1][0], ls[1][1], ls[1][2]};
        
        
        GzCoord localResult = {0.0f,0.0f,0.0f};
        
        float RdotE = vectorDotProduct(R,E);
        if (RdotE < 0) RdotE = 0;
        if (RdotE > 1) RdotE = 1;
        
        scalarMultiply(ls_intensity, 
                       powf(RdotE, render->spec), 
                       localResult);
        
        addVectors(localResult, AccumulatedSpecResult, AccumulatedSpecResult);        
    }
    
     
    if(mulByK)
        vectorMulElementByElement(render->Ks, AccumulatedSpecResult, col);
    else
    {
        col[RED]   = AccumulatedSpecResult[RED];
        col[GREEN] = AccumulatedSpecResult[GREEN];
        col[BLUE]  = AccumulatedSpecResult[BLUE];
        
    }
}
Exemplo n.º 4
0
void getTriplesByPerimeter(triple * t, const uint64_t limit, triple *** const list, uint32_t * const count, uint32_t * const size) {
  static const int64_t transformationMatrix[3][3][3] =
    {{{ 1,-2, 2}
     ,{ 2,-1, 2}
     ,{ 2,-2, 3}}
    ,{{ 1, 2, 2}
     ,{ 2, 1, 2}
     ,{ 2, 2, 3}}
    ,{{-1, 2, 2}
     ,{-2, 1, 2}
     ,{-2, 2, 3}}};

  uint32_t i;
  uint64_t perimeter_ = perimeter(t);

  if(perimeter_ > limit) {
    free(t);
    return;
  }

  addToList(t,list,count,size);
  for(i=2; i*perimeter_ <= limit; ++i)
    addToList(scalarMultiply(i,t),list,count,size);

  for(i=0; i<3; ++i)
    getTriplesByPerimeter(matrixMultiply((int64_t *)transformationMatrix[i],t)
                         ,limit,list,count,size);
}
Exemplo n.º 5
0
Point* Rectangle::Intersect(Vector v, Point o)
{
    // Check if vector is parallel to plane (no intercept)
    if (dotProduct(v, _normal) == 0)
    {
        return nullptr;
    }

    // Find the distance from the ray origin to the intersect point
    float distance = dotProduct(Vector(_a, o), _normal) / dotProduct(v, _normal);

    // From the distance, calculate the intersect point
    Point *intersect = new Point(scalarMultiply(v, distance).Translate(o));

    // Test to see if the point is inside the rectangle
    Vector v_test(*intersect, _c);
    Vector v1_test(_b, _c);
    Vector v2_test(_c, _d);

    if ( (0 <= dotProduct(v_test, v1_test)) &&
         (dotProduct(v_test, v1_test) < dotProduct(v1_test, v1_test)) &&
         (0 <= dotProduct(v_test, v2_test)) &&
         (dotProduct(v_test, v2_test) < dotProduct(v2_test, v2_test)))
    {
        return intersect;
    }
    else
    {
        // does not intersect plane within the rectangle
        return nullptr;
    }
}
Exemplo n.º 6
0
int main(void) {
    void scalarMultiply(int nRows, int nCols, int matrix[nRows][nCols],
                        int scalar);
    void displayMatrix(int nRows, int nCols, int martix[nRows][nCols]);
    int sampleMatrix[3][5] = {
        {7, 16, 55, 13, 12}, {12, 10, 52, 0, 7}, {-2, 1, 2, 4, 9}};

    printf("Original matrix:\n");
    displayMatrix(3, 5, sampleMatrix);

    scalarMultiply(3, 5, sampleMatrix, 2);
    printf("Matrix multipled by 2:\n");
    displayMatrix(3, 5, sampleMatrix);

    scalarMultiply(3, 5, sampleMatrix, -1);
    printf("Martix multiplied by -1:\n");
    displayMatrix(3, 5, sampleMatrix);

    return 0;
}
Exemplo n.º 7
0
//------------------------------------------------------------------------------
bool shouldCalcLight(GzCoord N, GzCoord L, GzCoord E, GzCoord Nnew) {
    float NdotL = vectorDotProduct(N, L);
    float NdotE = vectorDotProduct(N, E);
    bool shouldCalc = (NdotL * NdotE) > 0? true: false;
    
    if(NdotL < 0 && NdotE < 0)
    {
        scalarMultiply(N, -1.0f, Nnew);
    }
    
    return shouldCalc;
}
Exemplo n.º 8
0
//------------------------------------------------------------------------------
void calc_diffuse(GzRender *render, GzCoord N_orig, GzColor col, bool mulByK) {
    // N is already Ncm transformed.    
    GzCoord N = {0.0f,0.0f,0.0f};
    normalizeVector(N_orig, N);
    
    GzCoord AccumulatedDiffuseResult = {0.0f, 0.0f, 0.0f};
    
    for(int i = 0; i < render->numlights; i++)
    {
        float (*ld)[3] = static_cast<float (*)[3]>(render->lights[i]);
        GzCoord ld_L_orig = {ld[0][0], ld[0][1], ld[0][2]};       
        GzCoord ld_L = {0.0f,0.0f,0.0f};         
        normalizeVector(ld_L_orig, ld_L);

        GzCoord E = {0,0,-1};
        
        if (!shouldCalcLight(N, ld_L, E, N))
            continue;
        
        
        float N_dot_L = vectorDotProduct(N, ld_L);                
        GzCoord ld_intensity = {ld[1][0], ld[1][1], ld[1][2]}; 
        
        GzCoord localResult = {0.0f,0.0f,0.0f};
        scalarMultiply(ld_intensity, N_dot_L, localResult);
        addVectors(localResult, AccumulatedDiffuseResult, AccumulatedDiffuseResult);
    }
    
    
    if(mulByK)
        vectorMulElementByElement(render->Kd, AccumulatedDiffuseResult, col);
    else
    {
        col[RED]   = AccumulatedDiffuseResult[RED];
        col[GREEN] = AccumulatedDiffuseResult[GREEN];
        col[BLUE]  = AccumulatedDiffuseResult[BLUE];
        
    }
}
Exemplo n.º 9
0
//------------------------------------------------------------------------------
void calc_color(GzRender *render, GzCoord N_orig, GzColor col, bool mulByK)
{
    
    GzCoord N = ZEROS;
    normalizeVector(N_orig, N);
    
    
    Matrix *Ncm = render->nStack.leftMulMatricesOnStack();
    if (Ncm == NULL)
    {
        fprintf(stderr, "Got NULL from normal stack in %s.\n",__FUNCTION__);
    }

    
    float array[4] = {N[X], N[Y], N[Z], 1};
    float Ntransformed[4] = {0, 0, 0, 0};
    Ncm->rightMultiply(array, 4, Ntransformed);
    
    GzColor ambient = ZEROS;
    GzColor diffuse = ZEROS;
    GzColor specular = ZEROS;
    
    calc_ambient(render, ambient, mulByK);
    calc_diffuse(render, Ntransformed, diffuse, mulByK);
    calc_specular(render, Ntransformed, specular, mulByK);

    addThreeColors(ambient, diffuse, specular, col);

    render->km_ka = KM_KA;
    render->km_kd = KM_KD;
    render->km_ks = KM_KS;

    //////////////////////////////////////////////////////////////////////////////////////////////////////
    // THIS PART IS NEW FOR THE PROJECT
    // (the normal homework code is commented out above, all functions it calls are unmodified)

#if 1
    // For each light, determine what layers of paint to add to the model
    scalarMultiply(col, 0.0f, col);
    GzCoord ones = {1.0f, 1.0f, 1.0f};
    vcopy(col, ones);
    GzCoord background_reflectance = BACKGROUND_REFLECTANCE;
    GzCoord background_transmittance = BACKGROUND_TRANSMITTANCE;
    for (int i = 0; i < render->numlights; i++)
    {
        // This light"s direction
        float (*ls)[3] = static_cast<float (*)[3]>(render->lights[i]);
        GzCoord ls_L_orig = {ls[0][0], ls[0][1], ls[0][2]};

        // Ambient light color
        float (*lamb)[3] = static_cast<float (*)[3]>(render->ambientlight);
        GzColor lambcolor = {lamb[1][0], lamb[1][1], lamb[1][2]};

        // This light"s color
        GzColor ls_intensity = {ls[1][0], ls[1][1], ls[1][2]};
        // printVector(ls_intensity, "ls_intensity");
        // printVector(render->Ka, "Ka");
        // printVector(render->Kd, "Kd");
        // printVector(render->Ks, "Ks");

        GzCoord diffuse_thicknesses = {0.0f,0.0f,0.0f};
        calc_diffuse_thickness(render, Ntransformed, ls_L_orig, diffuse_thicknesses);
        // printVector(diffuse_thicknesses, "diffuse_thicknesses");
        GzCoord unlit_thicknesses = {0.0f,0.0f,0.0f};
        calc_unlit_thickness(render, Ntransformed, ls_L_orig, unlit_thicknesses);
        // printVector(unlit_thicknesses, "unlit_thicknesses");

        // printf("\n\n");
        // Use KM Model to add each of these layers of paint to the color of this vertex
        GzCoord diffuse_reflectance = ZEROS;
        GzCoord diffuse_transmittance = ZEROS;
        GzCoord unlit_reflectance = ZEROS;
        GzCoord unlit_transmittance = ZEROS;
        
        // printf("diffuse layer\n");
        GzCoord diffuse_color = DIFFUSE_COLOR;
        kubelka_munk(diffuse_color, diffuse_thicknesses, diffuse_reflectance, diffuse_transmittance);
        // printf("unlit layer\n");
        GzCoord unlit_color = AMBIENT_COLOR;
        kubelka_munk(unlit_color, unlit_thicknesses, unlit_reflectance, unlit_transmittance);
        // printf("\n\n");

        // vmult(diffuse_reflectance, ls_intensity, diffuse_thicknesses);
        // vmult(unlit_reflectance, ls_intensity, unlit_thicknesses);

        GzCoord composite_reflectance = ZEROS;
        GzCoord composite_transmittance = ZEROS;
        km_composite_layers(diffuse_reflectance, diffuse_transmittance, diffuse_reflectance, 
                                diffuse_transmittance, background_reflectance, background_transmittance);
        km_composite_layers(background_reflectance, background_transmittance, unlit_reflectance, 
                                unlit_transmittance, diffuse_reflectance, diffuse_transmittance);
        // printVector(diffuse_reflectance, "diffuse_reflectance");
        // vmult(col, col, lambcolor);
        
        // break;  // ASSUME ONLY ONE COLOR!!!
    }

    vcopy(col, background_reflectance);
#endif

}
Exemplo n.º 10
0
int main()
{
	Matrix *matA = NULL, *matB = NULL, *matC = NULL;
	char choice, which;
	int rows, cols, k;

	while (true) {
		menu();
		scanf("%c",&choice);
		getchar(); // ignore newline character
		choice = toupper(choice);

		if (choice == 'Q') {
			break;
		}

		switch (choice) {
			case 'C':	// create the matrices to work with
				do {
					printf("Enter number of rows and columns for Matrix A: ");
					scanf("%d%d", &rows, &cols);
					getchar();	// ignore newline character
					if (rows <= 0 || cols <= 0) {
						printf("Invalid dimensions. Try again...\n");
					}
				} while (rows <= 0 || cols <= 0);
				matA = create(rows,cols);
				load(matA);
				do {
					printf("Enter number of rows and columns for Matrix B: ");
					scanf("%d%d", &rows, &cols);
					getchar();	// ignore newline character
					if (rows <= 0 || cols <= 0) {
						printf("Invalid dimensions. Try again...\n");
					}
				} while (rows <= 0 || cols <= 0);
				matB = create(rows,cols);
				load(matB);
				break;
			case 'P':	// print the matrices
				if (matA == NULL || matB == NULL) {
					break;
				}
				printf("Matrix A:\n");
				print(matA);
				printf("Matrix B:\n");
				print(matB);
				break;
			case 'A':	// add matrices
				if (matA == NULL || matB == NULL) {
					break;
				}
				if (matA->rows == matB->rows && matA->columns == matB->columns) {
					matC = add(matA, matB);
					print(matC);
				} else {
					printf("Add failed: Matrix A and B do not have same size.\n");
				}
				break;
			case 'S':	// subtract matrices
				if (matA == NULL || matB == NULL) {
					break;
				}
				if (matA->rows == matB->rows && matA->columns == matB->columns) {
					matC = subtract(matA, matB);
					print(matC);
				} else {
					printf("Subtract failed: Matrix A and B do not have same size.\n");
				}
				break;
			case 'T':	// transpose a matrix
				if (matA == NULL || matB == NULL) {
					break;
				}
				printf("Transpose matrix A or B? (enter A or B): ");
				scanf("%c", &which);
				getchar();	// ignore newline character
				which = toupper(which);
				if (which == 'A') {
					matC = transpose(matA);
					print(matC);
				} else if (which == 'B') {
					matC = transpose(matB);
					print(matC);
				} else {
					printf("Invalid input.\n");
				}
				break;
			case 'K':	// multiply one of the matrices with a scalar value
				if (matA == NULL || matB == NULL) {
					break;
				}
				printf("Enter scalar value for k: ");
				scanf("%d", &k);
				getchar();	// ignore newline character
				printf("Scalar multiply matrix A or B? (enter A or B): ");
				scanf("%c", &which);
				which = toupper(which);
				getchar();	// ignore newline character
				if (which == 'A') {
					matC = scalarMultiply(matA, k);
					print(matC);
				} else if (which == 'B') {
					matC = scalarMultiply(matB, k);
					print(matC);
				} else {
					printf("Invalid input.\n");
				}
				break;
			case 'M':	// multiply the matrices
				if (matA == NULL || matB == NULL) {
					break;
				}
				if (matA->columns == matB->rows) {
					matC = multiply(matA, matB);
					print(matC);
				} else {
					printf("Multiply failed: Number of columns in Matrix A must be same as the number of rows in Matrix B.\n");
				}
				break;
		}	// end of switch
	}	// end of while

	printf("Bye.\n");
	return EXIT_SUCCESS;
}
Exemplo n.º 11
0
void Rectangle::Subdivide(float patchSize)
{
	if (mPatches == nullptr)
	{
		mPatches = new std::vector< Patch* >();

		// Calculate the number of patches along one axis
		float distance_i = _a.DistanceTo(_b);
		float dimension_i = distance_i / patchSize;
		int size_i = int(dimension_i);
		float remainder_i = dimension_i - size_i;

		if (remainder_i > 0)
		{
			++size_i;
		}

		// Calculate the number of patches along the other axis
		float distance_j = _a.DistanceTo(_d);
		float dimension_j = distance_j / patchSize;
		int size_j = int(dimension_j);
		float remainder_j = dimension_j - size_j;

		if (remainder_j > 0)
		{
			++size_j;
		}

		// Create a two-dimensional vector to hold points
		std::vector< std::vector<Point*> > points(size_i + 1, std::vector<Point*>(size_j + 1,
			(Point*)nullptr));

		Vector AB(_b, _a);
		Vector AD(_d, _a);
		float len_AB = _b.DistanceTo(_a);
		float len_AD = _d.DistanceTo(_a);

		normalize(AB);
		normalize(AD);

		Point *p1;

		// Create the starting point
		p1 = new Point(_a);

		// Loop in AD direction
		for (int j = 0; j <= size_j; ++j)
		{
			// add p1 to the list
			points.at(0).at(j) = p1;

			Point *p2 = p1;

			// Loop in AB direction
			for (int i = 0; i < size_i; ++i)
			{
			    Point *p3;

				// Check boundary
				if (i == size_i - 1)
				{
					p3 = new Point(scalarMultiply(AB, len_AB).Translate(*p1));
				}
				else
				{
					p3 = new Point(scalarMultiply(AB, patchSize).Translate(*p2));
				}

				// add p3 to the list
				points.at(i+1).at(j) = p3;

				// Update p2
				p2 = p3;
			}

			// Update p1
			if (j == size_j - 1)
			{
				p1 = new Point(scalarMultiply(AD, len_AD).Translate(_a));
			}
			else
			{
				p1 = new Point(scalarMultiply(AD, patchSize).Translate(*p1));
			}
		}

		// Create the patches
		// Loop in AD direction
		for (int j = 0; j < size_j; ++j)
		{
			// Loop in AB direction
			for (int i = 0; i < size_i; ++i)
			{
				Point *A = points.at(i).at(j);
				Point *B = points.at(i+1).at(j);
				Point *C = points.at(i+1).at(j+1);
				Point *D = points.at(i).at(j+1);

				// Create the patch
				Patch *p = new Patch(A, B, C, D, GetColor(), emission);
				mPatches->push_back(p);
			}
		}
	}
}