Jump to content
Dariuas

C++ Assistance needed

Recommended Posts

http://pastebin.com/VaYaHRaM

 

So that is the source; I need to take the code, and turn the program in to one that writes not in dotted lines as a parabola, but instead, as a solid line.

Yes this is homework.

 

Here is some code I was working up to try

 

 

// draw some pixels around the points entered by the user

PGraphics->AddPoint(x1+1, y1, color);

PGraphics->AddPoint(x1, y1+1, color);

PGraphics->AddPoint(x1+1, y1+1, color);

 

PGraphics->AddPoint(x2+1, y2, color);

PGraphics->AddPoint(x2, y2+1, color);

PGraphics->AddPoint(x2+1, y2+1, color);

 

// draw the points

PGraphics->Draw();

}

 

So it just dawned on me, perhapes, if I loop this with a counter, instead of just having it draw x1+1, y1, color

I could have it draw x1++, y1++, color

 

Thoughts?

Share this post


Link to post
Share on other sites

that's a part of C++ we never covered :(

Ditto there. Only thing I ever did drawing or graphics in was java.

Share this post


Link to post
Share on other sites

Although I can't say I did much with graphics in terms of C++ I'd say that some sort of loop spanning the length of the solid line would work using x1++ for horizontal and y1++ for vertical. One can go a bit further and create a whole bunch of simple functions, maybe 3 for lines (horizontal, vertical, diagonal). What exactly are you doing with these lines?

 

It's been a while since I've coded but maybe smth along the lines of:

 

void HorizontalLine(int l, int x1, int y1)

// starting at x1 and y1 draw a horizontal line of length l

{

for (int i=0; i<length; i++)

PGraphics->AddPoint(x1+i, y1, color);

}

 

void VerticalLine(int l, int x1, int y1)

// starting at x1 and y1 draw a vertical line of length l

{

for (int i=0; i<length; i++)

PGraphics->AddPoint(x1, y1+i, color);

}

 

the diagonal would be a bit more "complicated" as you'd have to differentiate between positive and negative slope (perhaps take in the slope or an angle and use basic trig). For bonus points one can have it plot more complicated functions. Hope this helps :cheers:

 

EDIT: I am an butt, and I finally clicked the link supplied. There seems to be a function there that is trying to draw a line between two points in the general case, not simply horizontal or vertical. Upon further inspection of that function it seems to me it only draws the endpoints of the line. I haven't coded anything in over 2 and a half years but give me a moment here. I'll see if I can make that function work.

Edited by PruritusAni

Share this post


Link to post
Share on other sites

SIMPLE METHOD. The only thing that quoted code does is to make the points bigger then a pixel (4 times as big), so they will be more visible. The way to simulate the function graph so it will seem like it was represented by a contigous line, is to variate your counter in the for-loop not by 1 unit, but by a small enough ratio so that when all points in the loop are drawn, because of the 72-96 dpi resolution of the screen, no spaces are left in the actual graph.

 

For example

 

75.     for (y = ymin; y <= ymax; y++) {
76.        x = (int)( a * (y-k) * (y-k) ) + h;
77.        PGraphics->AddPoint(x, y, green); 
78.    }

 

turns into something like this

 

75.     for (y = ymin; y <= ymax; y+=ratio) {
76.        x = ( a * (y-k) * (y-k) ) + h;
77.        PGraphics->AddPoint(x, y, green); 
78.    }

 

where ratio is

 

double ratio 0.01;

 

Your x's and y's become doubles also... you'll have to round them up for actual plotting.

 

Ofcourse I'm just guessing the ratio here, you should try 0.05, 0.1, 0.001, and others (you get the point, pun intended), you'll have to make a few simulations before you can have a small enough ratio that the graph seems contigous, but not too small that it takes a very long time to draw (optimize it so the number of actual pixels drawn is as small as possible).

 

You should also look into a method of scaling your plots, so that you get a good representation of you graph, but in a small enough area.

 

ANOTHER METHOD is to draw lines between EACH 2 consecutive coordinates, but at some point you'll get a somewhat jagged line, instead of a nice smooth curve.

Share this post


Link to post
Share on other sites

it definitely seems sihastru has got it well under control. I was thinking too much along the lines of pixel by pixel drawing a line. I was going to suggest an algorithm for drawing any line and then asking you if there was a thickness involved (from the 4 pixels thing the function does around the endpoints). Sihastru's method does seem a lot more elegant though.

Share this post


Link to post
Share on other sites

sorry for not making a new topic , i know that this is kind of off topic , but it is not worth a new one so here goes :

i made a simple program , the program is meant to take the ID from the user and display some info , then ask him if he wants to repeat that action once more ..

the thing is , i made it to ask the user to input "yes" or "no" depending on his wishes , now i have done that , it always terminates after asking him and taking the input

// on structures
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
struct Students {
int ID;
char name[80];
double GPA;
char email [1000];
};
int main()
{
   loop:{
   Students nour = {
    9103614,
     "Nour Eldin Ali",
     3.81,
     "[email protected]"
   };
   Students adham = {
   9103615,
   "Adham Ayman Zaki",
   3.91,
   "[email protected]"
   };
long ID;
cout<<"Enter the student's ID "<<endl;
cin>>ID;


 if (ID==9103614)
  cout<<"\n"<<nour.name<<"\n"<<nour.GPA<<"\n"<<nour.email<<endl;
else  if (ID==9103615)
  cout<<"\n"<<adham.name<<"\n"<<adham.GPA<<"\n"<<nour.email<<endl;
  else cout<<"\nIncorrect ID"<<endl;
   }
   char choice[50];

   for(;
   {
       cout<<"\ntype \"yes\"to view another student's inforamtion , type \"no\" to exit\n"<<endl;
       gets (choice);
       cout<<"\n"<<endl;
       if ( !strcmp(choice,"yes") )   // problem starts form here
        goto loop;
        else if (!strcmp(choice,"no"))
        goto stop;
        else goto stop;
   }
   stop:
  cin.get();
}


i have done this using code blocks

 

i will appreciate any help

Edited by N.E.A

Share this post


Link to post
Share on other sites

are there no == or != operators in the cstring class? Are you asking if the choice is not "yes"? There's a ! in there which negates the return value of strcmp in both conditional statments.

 

perhaps avoiding goto like you would the plague is a good idea after all. Try it with a while loop and a break if the guy says no:

 

int main ()
{
// declare your needed variables and stuff here

// brute force the two students here
// perhaps expansions of the program would allow for adding of other students and storing them in a data structure

while( true )
{
cout << " Enter the student's number" << endl;
cin >> ID;
if (ID == smth)
{
   //display that guy's stuff
}
else if (ID == smth else)
{
   //display this other guy's stuff
}
else
  cout << "student with " << ID << " not found" << endl;

cout << " Would you like to do this again?" << endl;
cin >> choice;
if (choice == "no" || choice == "No" || choice == "N")
  break;
else if ( choice != "yes" || choice != "Yes" || choice != "Y")
{
  cout << "invalid choice, program terminates now" << endl;
  return 0;
}
}

return 0;
}

Edited by PruritusAni

Share this post


Link to post
Share on other sites

are there no == or != operators in the cstring class? Are you asking if the choice is not "yes"? There's a ! in there which negates the return value of strcmp in both conditional statments.

 

perhaps avoiding goto like you would the plague is a good idea after all. Try it with a while loop and a break if the guy says no:

 

int main ()
{
// declare your needed variables and stuff here

// brute force the two students here
// perhaps expansions of the program would allow for adding of other students and storing them in a data structure

while( true )
{
cout << " Enter the student's number" << endl;
cin >> ID;
if (ID == smth)
{
   //display that guy's stuff
}
else if (ID == smth else)
{
   //display this other guy's stuff
}
else
  cout << "student with " << ID << " not found" << endl;

cout << " Would you like to do this again?" << endl;
cin >> choice;
if (choice == "no" || choice == "No" || choice == "N")
  break;
else if ( choice != "yes" || choice != "Yes" || choice != "Y")
{
  cout << "invalid choice, program terminates now" << endl;
  return 0;
}
}

return 0;
}

i think you did not get what i coded , and it seems that you made it more harder than i did , any way ,

what i did is take a string from the user

and compare it to another string , if it is the same it will return 0 (false) and with ! operator it will be true , and the next statement will be executed , if it is not the same it will return 1 (true) and with the ! operator it will be false and this way

the next statement will not be executed .. that is what i wanna do , it is not a home work or any thing like it , i like to think of programs and build it my self.

and my code is a bit easier to code and understand . i appreciate your outstanding effort

but i want to do with the string comparison function ( strcmp (s1,s2) ) .

i will explain what happened :

... (info)

type : yes for.... type no for ...

input : yes

compiler : input : yes , if statement : compare (chioce = yes to " yes " ) and then negate it

they are the same and i will return 0 (false) , after negating it, it is true , ok lets execute this : goto loop ;

that is what i want to happen

but the thing is , it seems like it compares it and then go out of it

i got this method from

C:\Users\MR. Brown\Desktop\Programming Tutorials\C++\C++ Beginner's Guide CH04.xps

page 14 ,

and this is what is there :

strcmp

A call to strcmp( ) takes this general form: strcmp(s1, s2); The strcmp( ) function compares two strings and returns 0 if they are equal. If s1 is greater than s2 lexicographically (that is, according to dictionary order), then a positive number is returned; if it is less than s2, a negative number is returned. The key to using strcmp( ) is to remember that it returns false when the strings match. Therefore, you will need to use the ! operator if you want something to occur when the strings are equal. For example, the condition controlling the following if statement is true when str is equal to “C++”: if(!strcmp(str, "C++") cout << "str is C++";

Edited by N.E.A

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×