plot in c++

What is the best/easy way to make a plot using c++ code?
in the other word:how we can run the c++ code using other program like(python) to make a plot? may anyone give me a simple example code to do that?

Thanks in advance.

Last edited on
I won't say my code is the best or easier to make. But this could be one of the way to plot a graph. My code was my assignment to make a line depending on the user input in a .pgm format.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <iostream>
#include <fstream>

/*This project was made by Talha Asif
ID:EE-131097 CLASS: 1-B*/

using namespace std;
const int sz=250;
int x[sz][sz];
string fname="abc.pgm";

void drawinfile(string fname)
{
    ofstream out(fname.c_str());
    // out.open();
    out << "P2 \n" << sz << " " << sz << " \n255 \n";
    for(int i=0;i<sz; i++)
    {
      for(int j=0;j<sz; j++)
        out << x[i][j] << " ";
       out<< endl;
    }
    out.close();
}

void line(int x1, int y1, int x2, int y2, int c)
{
    double deltax, deltay, m12, m, c12;
    deltax = x2 - x1;
    deltay = y2 - y1;
    m12 = (deltay / deltax);
    c12 = y2 - (m12 * x2);
    int *smallx, *smally, *bigx, *bigy;
    if(x1 > x2)
    {
        smallx = &x2;
        bigx = &x1;
    }
    else
    {
        smallx = &x1;
        bigx = &x2;
    }
    if(y1 > y2)
    {
        smally = &y2;
        bigy = &y1;
    }
    else
    {
        smally = &y1;
        bigy = &y2;
    }
    if(m12 != 0)
    {
        for(int y = *smallx;y <= *bigx;y++)
        {
            for(int z = *smally;z <= *bigy;z++)
            {
                    int a = ((m * y) + c12);
                    x[a][z] = c;
            }
        }
    }
    else
    {
        for(int y = *smallx;y <= *bigx;y++)
        {
            for(int z = *smally;z <= *bigy;z++)
            {
                    int a = c12;
                    x[a][z] = c;
            }
        }
    }
}

void init()
{
    for(int i=0;i<sz; i++)
      for(int j=0;j<sz; j++)
        x[i][j]=0;

}
int main()
{
    int choice, x1, y1, x2, y2, c;
    init();
    cout << "Enter what you want as output\n" << "1) Single line\n" << "2) Triangle" << endl;
    cout << "Enter your desired choice" << endl;
    cin >> choice;
    while(!(choice == 1 || choice == 2))
    {
        cout << "Please enter a valid choice again" << endl;
        cin >> choice;
    }
    switch(choice)
    {
    case 1:
        {
            cout << "Enter the coordinate of the starting point in the sequence x y seperating them by a space" << endl;
            cin >> x1 >> y1;
            cout << "Enter the coordinate of the ending point in the sequence x y seperating them by a space" << endl;
            cin >> x2 >> y2;
            cout << "Enter the c" << endl;
            cin >> c;
            line(x1, y1, x2, y2, c);
        }
        break;
    default:
        {
            for(int a = 0;a != 3;a++)
            {
                cout << "Enter the coordinate of the starting point in the sequence x y seperating them by a space" << endl;
                cin >> x1 >> y1;
                cout << "Enter the coordinate of the ending point in the sequence x y seperating them by a space" << endl;
                cin >> x2 >> y2;
                cout << "Enter the c" << endl;
                cin >> c;
                line(x1, y1, x2, y2, c);
            }
        }
    }
    drawinfile("abc.pgm");
    return 0;
}
I made a grapher using SFML a while ago.
http://www.sfml-dev.org/

It's not too difficult.
Stalker, but its very difficult to understand and see how you made your plot.
Thanks.
integralfx , thank you too, I will take a look to SFML grapher page.
it is possible to save the output result in c++ as a text file and use this text file to plot it in python for example
Yes.
I got it now thanks,I made a text file and I plot it in python
Topic archived. No new replies allowed.