Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* File : justAnotherDisc */
- /* Author : Vasil Gevezov, Lauri Lujala */
- /* Date : 01.10.2014 */
- /* version : 9000.1 */
- /* Description :
- * This is a program that checks for overlap between discs. */
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- /* This structure stores all the needed data per circle in a single abstract data type. */
- typedef struct discInfo{
- int x, y, r;
- }discInfo;
- /* safeMalloc used to ensure that the memory is allocated properly. */
- void *safeMalloc(int arraySize) {
- void *ptr = malloc(arraySize);
- if (ptr == NULL) {
- printf("\nError: mAllocation failed.\n");
- exit(-1);
- }
- return ptr;
- }
- /* Now the allocated memory will be filled with the values from the input file. */
- discInfo *fillArray(int arraySize){
- discInfo *discArray = (discInfo*)safeMalloc(arraySize*sizeof(discInfo*));
- discInfo content;
- int i;
- for ( i = 0; i < arraySize; i++){
- scanf("%d %d %d", &content.x, &content.y, &content.r);
- discArray[i] = content;
- }
- return discArray;
- }
- /* 5:55 */
- int overlap(discInfo *discArray, int discAmount){
- int contact = 0, i, u;
- int noContact;
- for (i = 0;i < discAmount; i++){
- for (u = 0; u < discAmount; u++){
- if ((sqrt(((discArray[i].x - discArray[u].x)*(discArray[i].x - discArray[u].x))
- + ((discArray[i].y - discArray[u].y)*(discArray[i].y - discArray[u].y)))
- <= discArray[i].r + discArray[u].r) && u != i){
- contact++;
- }
- }
- }
- noContact = (discAmount - contact);
- return noContact;
- }
- int main(int argc, char argv[]){
- int arraySize;
- int noContact = overlap(discArray, arraySize);
- scanf("%d", arraySize);
- discInfo *discArray = (discInfo*)safeMalloc(arraySize*sizeof(discInfo*));
- printf("number of discs: %d\n", arraySize);
- printf("number of non-overlapping discs: %d\n", overlap);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement