Thread: Help With Linked List Code

  1. #1
    Registered User
    Join Date
    Nov 2016

    Help With Linked List Code

    I am trying to write a program that counts how many points are within the radius of a circle. The user chooses the x and y coordinates of the center of the circle, the radius of the circle, the x and y coordinates of the points, and the program responds with how many of those points are in the circle. This is what I've tried, but it only counts the total number of points. Can't figure out what I'm doing wrong!
    typedef struct {
        float x, y;
    } Point;
    /* PointListNode -- A node of a singly-linked list of points */
    typedef struct PointListNode{
        Point *p;
        struct PointListNode *next;
    } PointListNode;
    int count_nearby_points(PointListNode *pointList, Point* centrePoint, float radius){
    	PointListNode* current = pointList;
    	float xDistance, yDistance, length;
    	int count=0;
    	while (current != NULL){
    		xDistance = (pointList->p->x) - (centrePoint->x) ;
    		yDistance = (pointList->p->y) - (centrePoint->y) ;
    		length = sqrt((xDistance*xDistance)+(yDistance*yDistance));
    		if(length <= radius){
    			current = current->next;
    	}return count;
    } /* count_nearby_points */

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    United States
    If the center point is not at the origin you need the general form of the circle equation. You already know r2 = x2 + y2. Well the general form is
    r2 = (x-h)2 + (y-k)2
    where (h,k) is the center point.

    This means that you can calculate a certain distance and see if it's less than a given diameter.
    if ((x-h) * (x-h) + (y-k) * (y-k) < r * r) 
       puts("The point is in the circle");

  3. #3
    Registered User
    Join Date
    Nov 2016
    Is that not what line 16-20 of my code does though?

  4. #4
    Registered User
    Join Date
    Jun 2015
    In the listing above on lines 16 and 17, you're accidentally using pointList where you should be using current. So you're only really just testing the first element over and over as current moves through the list.

  5. #5
    Registered User
    Join Date
    Nov 2016
    That fixed it! I knew it was just something simple I was missing. Thankyou!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need help with my code [linked list]
    By Pipo1990 in forum C Programming
    Replies: 5
    Last Post: 05-23-2015, 05:49 AM
  2. Help with debugging my Linked List code.
    By nslice22 in forum C Programming
    Replies: 8
    Last Post: 03-26-2014, 03:23 PM
  3. Sorting linked list please help with CODE
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 09-27-2008, 11:24 PM
  4. Linked list - why this bit of code?
    By chris1985 in forum C Programming
    Replies: 2
    Last Post: 10-04-2005, 06:17 AM
  5. Linked List Working Code
    By Linette in forum C++ Programming
    Replies: 9
    Last Post: 01-24-2002, 12:00 PM

Tags for this Thread