Advertisement
Guest User

Untitled

a guest
Oct 2nd, 2014
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. /* File : justAnotherDisc */
  2. /* Author : Vasil Gevezov, Lauri Lujala */
  3. /* Date : 01.10.2014 */
  4. /* version : 9000.1 */
  5.  
  6. /* Description :
  7. * This is a program that checks for overlap between discs. */
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <math.h>
  12.  
  13. /* This structure stores all the needed data per circle in a single abstract data type. */
  14. typedef struct discInfo{
  15. int x, y, r;
  16.  
  17. }discInfo;
  18.  
  19. /* safeMalloc used to ensure that the memory is allocated properly. */
  20. void *safeMalloc(int arraySize) {
  21. void *ptr = malloc(arraySize);
  22. if (ptr == NULL) {
  23. printf("\nError: mAllocation failed.\n");
  24. exit(-1);
  25. }
  26. return ptr;
  27. }
  28. /* Now the allocated memory will be filled with the values from the input file. */
  29. discInfo *fillArray(int arraySize){
  30.  
  31. discInfo *discArray = (discInfo*)safeMalloc(arraySize*sizeof(discInfo*));
  32.  
  33. discInfo content;
  34. int i;
  35. for ( i = 0; i < arraySize; i++){
  36. scanf("%d %d %d", &content.x, &content.y, &content.r);
  37. discArray[i] = content;
  38. }
  39. return discArray;
  40. }
  41. /* 5:55 */
  42.  
  43. int overlap(discInfo *discArray, int discAmount){
  44.  
  45. int contact = 0, i, u;
  46. int noContact;
  47.  
  48. for (i = 0;i < discAmount; i++){
  49. for (u = 0; u < discAmount; u++){
  50. if ((sqrt(((discArray[i].x - discArray[u].x)*(discArray[i].x - discArray[u].x))
  51. + ((discArray[i].y - discArray[u].y)*(discArray[i].y - discArray[u].y)))
  52. <= discArray[i].r + discArray[u].r) && u != i){
  53. contact++;
  54. }
  55. }
  56. }
  57.  
  58. noContact = (discAmount - contact);
  59.  
  60. return noContact;
  61. }
  62.  
  63. int main(int argc, char argv[]){
  64.  
  65. int arraySize;
  66. int noContact = overlap(discArray, arraySize);
  67. scanf("%d", arraySize);
  68.  
  69. discInfo *discArray = (discInfo*)safeMalloc(arraySize*sizeof(discInfo*));
  70.  
  71. printf("number of discs: %d\n", arraySize);
  72. printf("number of non-overlapping discs: %d\n", overlap);
  73.  
  74. return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement