static inline int findIntersectionInternal(struct OBJ_Model * obj,Face triangle, Vertex p1, Vertex p2, Vector * new_normal, Vector * intersection_point) { Vector e1, e2, p, s, q; Vector bcoords; float t,u,v, tmp, e=0.000001; double l, new_normal_length; Vector v0,v1,v2,dir; Vector origin, end; origin.n1 = p1.x; origin.n2 = p1.y; origin.n3 = p1.z; end.n1 = p2.x; end.n2 = p2.y; end.n3 = p2.z; Subtraction(dir,end,origin); l = VectorLength(dir); dir.n1/=(float)l; dir.n2/=(float)l; dir.n3/=(float)l; v0.n1 = obj->vertexList[ triangle.v[0]].x; v0.n2 = obj->vertexList[ triangle.v[0]].y; v0.n3 = obj->vertexList[ triangle.v[0]].z; v1.n1 = obj->vertexList[ triangle.v[1]].x; v1.n2 = obj->vertexList[ triangle.v[1]].y; v1.n3 = obj->vertexList[ triangle.v[1]].z; v2.n1 = obj->vertexList[ triangle.v[2]].x; v2.n2 = obj->vertexList[ triangle.v[2]].y; v2.n3 = obj->vertexList[ triangle.v[2]].z; Subtraction(e1, v1, v0); Subtraction(e2, v2, v0); CrossProduct(p, dir, e2); tmp = DotProduct(p, e1); if (tmp > -e && tmp < e) return 0; tmp = 1.0/tmp; Subtraction(s, origin, v0); u = tmp * DotProduct(s, p); if (u<0 || u>1) return 0; CrossProduct(q, s, e1); v = tmp * DotProduct(dir, q); if (v<0 || v>1) return 0; if ( u+v >1) return 0; t = tmp * DotProduct(e2, q); bcoords.n2 = u; bcoords.n3 = v; bcoords.n1 = 1 - u - v; new_normal->n1 = bcoords.n1 * obj->normalList[ triangle.n[0]].n1 + bcoords.n2 * obj->normalList[ triangle.n[1]].n1 + bcoords.n3 * obj->normalList[ triangle.n[2]].n1; new_normal->n2 = bcoords.n1 * obj->normalList[ triangle.n[0]].n2 + bcoords.n2 * obj->normalList[ triangle.n[1]].n2 + bcoords.n3 * obj->normalList[ triangle.n[2]].n2; new_normal->n3 = bcoords.n1 * obj->normalList[ triangle.n[0]].n3 + bcoords.n2 * obj->normalList[ triangle.n[1]].n3 + bcoords.n3 * obj->normalList[ triangle.n[2]].n3; Vector tmpVec = *new_normal; new_normal_length = VectorLength(tmpVec); new_normal->n1 /=new_normal_length; new_normal->n2 /=new_normal_length; new_normal->n3 /=new_normal_length; intersection_point->n1 = dir.n1 * t + origin.n1; intersection_point->n2 = dir.n2 * t + origin.n2; intersection_point->n3 = dir.n3 * t + origin.n3; return 1; }
void main() // main function { int menu, result[100], total=0, count=0; char con; struct tm time; _getsystime(&time); int hr, min, sec; hr = time.tm_hour; min = time.tm_min; sec = time.tm_sec; int i=0; do { // Menu system("CLS"); printf("_-_-_-_Menu_-_-_-_\n" "[1] Multiplication\n" "[2] Division\n" "[3] Addition\n" "[4] Subtraction\n" "[5] Quit\n\n"); printf("Choose a selection > "); scanf("%d",&menu); switch(menu) // switch statement { case 1: { result[i] = Multiplication(); // function call Multiplication(); output(result[i]); // function call output(); count++; // the times of test break; } case 2: { result[i] = Division(); // function call Division(); output(result[i]); // function call output(); count++; // the times of test break; } case 3: { result[i] = Addition(); // function call Addition(); output(result[i]); // function call output(); count++; // the times of test break; } case 4: { result[i] = Subtraction(); // function call Subtraction(); output(result[i]); // function call output(); count++; // the times of test break; } case 5: { Quit(total,count,hr,min,sec); // function call Quit(); break; } default: { printf("Invalid Input! Please re-enter\n"); main(); // re-run the main function } i++; } total = total + result[i]; // sum all the tests' marks // ask for user want to continue the test or not printf("Do you want to continue? (Y/N) "); fflush(stdin); scanf("%c",&con); toupper(con); while(con!='Y' && con!='N' && con!='y' && con!='n') { printf("Invalid Input! Please re-enter (Y/N)> "); fflush(stdin); scanf("%c",&con); toupper(con); } }while(con == 'Y' || con=='y'); Quit(total,count,hr,min,sec); // Print out the total result & Quit the program }
int main() { // Part One: // FIXME: Declare variables // Declare two variables: an integer named "age", and a string named "name" with corresponding values (your name and age) int age = 19; char name[] = "Stan"; // FIXME: Print // Print the following sentence in the console "You are NAME and you are AGE years old !". Don't forget to add a newline at the end printf("You are %s and you are %d years old !\n", name, age); // FiXME: Concatenation // Create a new string variable called "hello" which value is "Hello ". Add "name" at the end of "hello" (Concatenation) then print it char hello[] = "Hello "; strcat(hello, name); printf("%s\n", hello); // FIXME: Array // create a new string array called "shoppingList", with three elements of your choice. Create an int variable containing the number of // elements in "shoppingList" (using a function of the array/using the array) char *shoppingList[3] = {"milk", "a Chevy Camaro", "a life"}; const int nbOfElements = numberOfElements(shoppingList); // FIXME: For-loop - Integer // Create a simple for-loop for an integer "i" going from 1 to 10 that print the value of "i" for (int i = 0; i < 10; i++) printf("%d ", i); printf("\n"); // FIXME: For-loop - shoppingList // Create a for loop that iterate through "shoppingList" and prints each element with "You have to buy (elemt)". for (int j = 0; j <nbOfElements; j++) printf("You have to buy %s\n", shoppingList[j]); // FIXME: Foreach-loop // Do the same with a foreach-loop. // The C programming language doesn't have a foreach, but you can look for macros if you are // curious. // FIXME: If-statement // Modify the first for-loop (with i from 1 to 10) such that it prints "(value of i) is even" when "i" is divisible // by 2 (You may want to learn more about "modulo" (%)). Else, print "(value of i) is odd". for (int i = 0; i < 10; i++) { if (i % 2) printf("%d is odd\n", i); else printf("%d is even\n", i); } // FIXME: Sum Up // Create a string variable called "element" with the value of your choice. Then create a for-loop/foreach-loop that checks if "shoppingList" // contains "element". If yes, print "You have to buy (value of element) !", and stop the loop (search how to stop a loop). // If not, print "Nope, you don't need (value of "element")". char element[] = "a life"; for (int k = 0; k < nbOfElements; k++) { if (strcmp(element, shoppingList[k]) == 0) { printf("You have to buy %s !\n", shoppingList[k]); break; } else { printf("Nope, you don't need %s !\n", shoppingList[k]); } } // Part Two: // FIXME: Functions - Ascii // Create a function that returns nothing and which doesn't takes any parameter. It should just be named "TriForce" // and print the TriForce symbol (one triangle over two other ones, can be found on internet) with "TRIFORCE" // Don't forget to call the function ! TriForce(); //Calling the functions in main() // FIXME: Functions - One parameter // Create a function that takes a string as parameter and returns "Hello (value of string) !" // http://stackoverflow.com/questions/1745726/how-to-store-printf-into-a-variable printf("%s", Hello("Stan")); // FIXME: Functions - Multiple parameters // Create a function that takes two integers as parameters and returns the addition of these two. // You can do the same with multiplication, subtraction and division. printf("%d\n", Addition(5, 12)); printf("%d\n", Subtraction(5, 12)); printf("%d\n", Multiplication(5, 12)); printf("%d\n", Division(5, 12)); // returns 0 because "a" and "b" are Integers printf("%d\n", Division(10, 2)); // FIXME: User entry // Create a string variable that takes what the user enter in the console as value. Then print "You entered (value of string)" char userInput[100]; // 100 will be the max length of the input printf("Enter a word:"); scanf("%s", userInput); printf("You entered %s\n", userInput); // FIXME: While loop // Create a while loop that takes a number and divides it by 2 until it is less than 3 int number = 52; while (number > 3) { number /= 2; //or number = number / 2; printf("%d ", number); } printf("\n"); // FIXME: do-While loop // Do the same with a do-while loop int number2 = 84; do { number2 = number2 / 2; printf("%d ", number2); } while (number2 > 3); printf("\n"); // FIXME: Random generator // Create a function that returns a random number srand((unsigned)time(NULL)); // time used for seeding printf("%d\n", rdm()); // FIXME: Random generator with bounds // Create another function that returns a random number between two bounds given as parameters. printf("%d\n", rdmBounds(10, 20)); // FIXME: Multi-dimensionnal array // Create a two dimensionnal int array of 3 columns and 3 rows. Use 2 for-loops to add a random number // between 1 and 9 in each of the 9 rooms. // You may use one of the two previously created function. // Then print them such that they appear like this (with [x1, x9] being the 9 random integers): // {x1, x2, x3, } // {x4, x5, x6, } // {x7, x8, x9, } int multiArray[3][3]; for (int l = 0; l < numberOfElements(multiArray); l++) for (int m = 0; m < numberOfElements(multiArray[l]); m++) multiArray[l][m] = rdmBounds(1, 9); for (int n = 0; n < numberOfElements(multiArray); n++) { printf("{"); for (int o = 0; o < numberOfElements(multiArray[n]); o++) printf("%d, ", multiArray[n][o]); printf("}\n"); } // FIXME: Switch // Create a Switch that takes an integer "a" and return a sentence regarding the value of a // (Create 3 statements for 3 specific values and a default one) int a = 2; switch(a) { case 0: printf("a is 0\n"); break; case 2: printf("a is 2\n"); break; case 37: printf("Who cares about a anyway ?"); break; default: printf("I don't know what \"a\" is... :'(\n"); break; } // FIXME: logic Gates // Create 7 functions for each logic gates (And (0), Or (1), No (2), Nand (3), Nor (4), Xnor (5), Xor (6)). // Each function takes two booleans as parameters and returns the result of the logic gate. // (or You can do it with a switch and only one function) logic_gates(1, 0, 1); logic_gates(1, 1, 1); logic_gates(1, 2, 1); // FIXME - Reverse // Create a function that reverse a string reverse("Hello world"); return 0; }