0

i have made a c++ program to read some 2d values (x,y) from a text file . My code is as follows:

#include<iostream>
#include<sstream>
#include<fstream>
using namespace std;

template <typename T>
class Node {
public:
    T data;
    Node<T>* next;
    Node<T>* previous;

    Node(T data) {
        this->data = data;
        this->next = NULL;
        this->previous = NULL;
    }
};

template <typename T>
class List {
public:
    int size;
    Node<T>* start;
    List() {
    start = NULL;
    size = 0;
}

Node<T>* insert(T data) {
    Node<T>* new_node = new Node<T>(data);
    new_node->next = start;
    new_node->previous = NULL;
    if (start != NULL) {
        start->previous = new_node;
    }
    start = new_node;

    size += 1;

    return new_node;
 }

};

class Point { 
public:
    double x;
    double y;
    Node<Point*>* points_node; 
    Point(double x, double y) {
    this->x = x;
    this->y = y;
  }

};

main()
{
    List<Point*>* input_points;
    input_points = new List<Point*>();
    ifstream ifs("input.txt");
    double x,y;
    ifs>>x>>y;
    while(!ifs.eof())
    {
        Point* p = new Point(x, y);
        input_points->insert(p);
        ifs>>x>>y;
    } 
   bool line=check_line(input_points); // boolean function not defined
   bool circle=check_circle(input_points); // boolean function not defined


} 

is there any way to write a boolean function to determine if all the points lie on a line or on a circle or not?

Input file format is as follows:

5.0 10.0
10.0 10.0
15.0 10.0
user5411115
  • 101
  • 1
  • 9
  • Possible duplicate: [See if a point lies on a line](http://stackoverflow.com/questions/26849632/see-if-a-point-lies-on-a-linevector) – Thomas Matthews Nov 29 '16 at 17:30
  • You should use `std::list` or `std::vector` instead of writing your own linked list. – Thomas Matthews Nov 29 '16 at 17:31
  • but what about points on circle? @Thomas Matthews – user5411115 Nov 29 '16 at 17:32
  • You need 2 points to make a line, and 3 points to fix a circle, once you create the equations for the line and circle you can start plugging the rest of the points into the equations and see if they match – Zukaberg Nov 29 '16 at 17:32
  • Where is `boolean` defined? The C++ language has `bool` type predefined, so you should use it. – Thomas Matthews Nov 29 '16 at 17:32
  • Off-topic: You can get rid of the `this->` notation by choosing different names between data members and parameters. – Thomas Matthews Nov 29 '16 at 17:33
  • sorry for mistake. edited now @Thomas Matthews – user5411115 Nov 29 '16 at 17:34
  • There are a lot of questions about points and circles. Try searching the internet for "stackoverflow c++ point on circle". I found the other topics by using a search "stackoverflow c++ point lies on line". – Thomas Matthews Nov 29 '16 at 17:36
  • I don't understand why you need to dynamically allocate the `List` in `main`. The C++ language is not Java or C#, most of the time, dynamical allocation is not necessary (one exception is dynamically allocating nodes in a list). – Thomas Matthews Nov 29 '16 at 17:38
  • Off-Topic: see [Why eof in a while statement is considered wrong](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). – Thomas Matthews Nov 29 '16 at 17:39

2 Answers2

1

Your list and program can use much improvement.

Using existing data structures
You can create a Point by using the std::pair class:

typedef std::pair<double> Point;

You can also use std::list instead of creating your own (which needs work):

typedef std::list<Point> Point_List;

Knowing this, the main function becomes:

int main(void)
{
  Point_List  data_points;
  ifstream    input("input.txt");
  if (!input)
  {
    cerr << "Error opening input.txt\n";
    return EXIT_FAILURE;
  }
  double x, y;
  while (input >> x >> y)
  {
    Point p;
    p.first = x;
    p.second = y;
    data_points.push_back(p);
  }
  if (line_check(data_points))
  {
    //...
  }
  if (circle_check(data_points))
  {
    //...
  }
  cout << "\n\nPaused.  Press Enter to continue.\n";
  cin.ignore(100000, '\n');
  return EXIT_SUCCESS;
}

You can use iterators to access each element in the list. Search the web for "c++ list iterator example".

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
0

For a line, pick two points and derive an equation from them in the form y = mx + b (solve for m and b using any of the two points) then see if the other points satisfy the equation too. If any do not, the points are not all on a line.

For a circle, the equation form is (x + a)^2 + (y + b)^2 = r^2 so again, take 3 points and solve for a, b, and r. Then see if the other points satisfy that equation too. If they all do, the points are on a circle.

An array is more appropriate here than a linked list, I think. You can use std::vector.

Andrew
  • 518
  • 2
  • 9