I tried searching for awnsers, but all the threads are different langs.
#include <cstdlib>
#include <iostream>
#include <math.h>
using namespace std;
void calcDistance (int x1, int y1, int x2, int y2);
int main()
{
int x1, y1, x2, y2;
cout << "Enter the points in coordinate pair form, ommiting parantheses" << endl;
cin >> x1 >> y1 >> x2 >> y2;
calcDistance (x1, y1, x2, y2);
system("pause");
// how do I cout the dist in main-- says dist isn't declared
}
void calcDistance (int x1, int y1, int x2, int y2)
{
int sideA;
sideA = x2 - x1;
int sideB;
sideB = y2 -y1;
int sideAsqd;
sideAsqd = sideA * sideA;
int sideBsqd;
sideBsqd = sideB * sideB;
int sideCsqd;
sideCsqd = sideAsqd + sideBsqd;
double dist;
dist = sqrt(sideCsqd);
cout << "The calculated distance is "<< dist << endl;
}
How do I make the second cout occur in main. I try just putting it in main, but then I get an error saying that dist is not declared in the scope.
I want to be able to use the dist value in main, while it has been calculated in the function.
Change your function:
double calcDistance (int x1, int y1, int x2, int y2)
{
int sideA = x2 - x1;
int sideB = y2 -y1;
int sideAsqd = sideA * sideA;
int sideBsqd = sideB * sideB;
int sideCsqd = sideAsqd + sideBsqd;
double dist = sqrt(sideCsqd);
return dist;
}
And in main do this:
double res = calcDistance (x1, y1, x2, y2);
cout << "The calculated distance is "<< res << endl;
Given a function, say
void calcDistance (int x1, int y1, int x2, int y2)
{
//...
double dist;
//...
}
The variable dist goes out of scope at the closing brace, so not only can you not refer to it from elsewhere, it won't exist when outside the function.
If you want the value somewhere else, return it:
double calcDistance (int x1, int y1, int x2, int y2)
{
//...
double dist;
//...
return dist;
}
To use it elsewhere just capture the return:
double distance = calcDistance(1,2,3,4);
Now you have another local variable called distance that you can use.
Related
I'm a neophyte with c++. I wrote this code but the result for q have to be 1.0, but the code give me, changing the variable's order when I recall function "intercetta", for example -34, 0, 9.75. Why?
#include <iostream>
using namespace std;
float coefficienteAngolare(float x1, float x2, float y1, float y2, float m) {
return m = ((y2 - y1) / (x2 - x1));
}
float intercetta(float m, float x1, float y1, float q) {
return q = y1 - m * x1;
}
int main() {
float x1, x2, y1, y2, m=0, q=0;
x1 = 3.5;
x2 = 6.5;
y1 = 9.75;
y2 = 17.25;
cout << "m= " << coefficienteAngolare(x1, x2, y1, y2, m) << endl;
cout << "q= " << intercetta(x1, y1, m, q) << endl;
}
This function
float coefficienteAngolare(float x1, float x2, float y1, float y2, float m) {
return m = ((y2 - y1) / (x2 - x1));
}
has parameters passed by value. It means that it receives copies of the parameters you give. Whatever you do inside the function, cannot alter the parameters passed to it in main().
If you really want to modify m, you have to pass it by reference
float coefficienteAngolare(float x1, float x2, float y1, float y2, float& m) {
return m = ((y2 - y1) / (x2 - x1));
}
But then, if you modify m, why do you need to return it?
Most probably you either want to not return anything and just store the result in m
void coefficienteAngolare(float x1, float x2, float y1, float y2, float& m) {
m = ((y2 - y1) / (x2 - x1));
}
//....
// in main()
coefficienteAngolare(x1, x2, y1, y2, m);
cout << "m= " << m << endl;
Or you want to return the resulting value, without passing a variable to store it.
float coefficienteAngolare(float x1, float x2, float y1, float y2) {
return ((y2 - y1) / (x2 - x1));
}
//....
// in main()
m = coefficienteAngolare(x1, x2, y1, y2);
cout << "m= " << m << endl;
Along the same line you have to modify intercetta.
Please notice that the order of the parameters is relevant. The compiler cannot guess that the q variable in main() should be the same as the q variable in intercetta, they belong to different scopes.
The variable m (and q) in your main function are different variables than the variables in your other functions. The assignment you have after your return statement assigns a value to a variable which has its lifetime limited to the respective function's scope.
If you want to pass-by-reference, you can do this by declaring the argument as a reference:
float intercetta(float m, float x1, float y1, float& q) {
// ^-------- reference
I have this assignment that asks to use functions in order to calculate the distance and slope of two points. The thing is I don't really understand how I can use a single function for two separate variables(has to be done this way for the assignment). In this case, the formula asks for x1 and x2, but I have to use a single function. So my question is how can I use my getX and getY functions that prompts for x and y twice? Basically use a single function for both first and second values. Hope that makes more sense.
/*Include statements*/
#include <stdio.h>
#include <math.h>
/*Given Prototypes*/
double getX();
double getY();
double distance(double, double, double, double);
double slope(double, double, double, double);
// begin main
int main()
{
// declare variables
double x1;
double x2;
double y1;
double y2;
// read in variables for points on a graph
x1 = getX();
y1 = getY();
x2 = getX();
y2 = getY();
// print out the distance between the points and the slope of the line
printf("\n");
printf("Distance between the points is %.2f \n", distance(x1, x2, y1, y2));
printf("Slope of the line is %.2f \n\n", slope(x1, x2, y1, y2));
}
/* begin getX function */
double getX()
{
double x;
printf( "Please enter the value of x:");
scanf("%lf", &x);
return x;
}
/* begin getY function */
double getY()
{
double y;
printf("Please enter the value of y:");
scanf("%lf", &y);
return y;
}
/* header for distance function */
double distance(double x1, double y1, double x2, double y2)
{
return sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
}
/* header for slope function */
double slope(double x1, double y1, double x2, double y2)
{
return (y2 - y1) / (x2 - x1);
}
you can use the "Struct" in C this will define new datatype that can sotre two or more separate values as below code
-- please if you have any questions
Code is Here :
/Include statements/
#include <stdio.h>
#include <math.h>
/*Given Prototypes*/
double getX();
double getY();
double distance(double, double, double, double);
double slope(double, double, double, double);
// begin main
int main()
{
// declare variables
double x1;
double x2;
double y1;
double y2;
// read in variables for points on a graph
x1 = getX();
y1 = getY();
x2 = getX();
y2 = getY();
// print out the distance between the points and the slope of the line
struct SlopDistance FinalValues;
FinalValues = CalcuateValues(x1, y1, x2, y2);
printf("\n");
printf("Distance between the points is %.2f \n", FinalValues.Distance);
printf("Slope of the line is %.2f \n\n", FinalValues.Distance);
}
/* begin getX function */
double getX()
{
double x;
printf("Please enter the value of x:");
scanf_s("%lf", &x);
return x;
}
/* begin getY function */
double getY()
{
double y;
printf("Please enter the value of y:");
scanf_s("%lf", &y);
return y;
}
/* header for distance function */
double distance(double x1, double y1, double x2, double y2)
{
return sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
}
/* header for slope function */
double slope(double x1, double y1, double x2, double y2)
{
return (y2 - y1) / (x2 - x1);
}
struct SlopDistance
{
double Slope;
double Distance
};
struct SlopDistance CalcuateValues(double x1, double y1, double x2, double y2)
{
struct SlopDistance FinalValue;
FinalValue.Distance = distance(x1, x2, y1, y2);
FinalValue.Slope = slope(x1, x2, y1, y2);
return FinalValue;
}
I'm writing a C++ graphic program. I need to translate, scale and rotate this rhombus shape (as well as other shapes). I want to write a function to do that but I've not figured out how to pass x1, y1, x2, y2 from the mouse inputs(or keyboard inputs) to the new transformation function I want to write.
I want to draw a shape on BGI screen, then click the 'translate' button, then enter some variables and the shape transforms.
FYI, I use BGI to do the graphics. Some shapes I draw with mouse inputs, some with keyboard inputs.
Here is my code:
void drawRhombus (int x1, int y1, int x2, int y2)
{
int x3 = x2 + (x2 - x1);
int y3 = y1;
int x4 = x2;
int y4 = y1 + (y1 - y2);
lineBresenham(x1, y1, x2, y2);
lineBresenham(x2, y2, x3, y3);
lineBresenham(x3, y3, x4, y4);
lineBresenham(x4, y4, x1, y1);
}
if (LOWORD(wParam) == 9) {
cout<<"Draw Rhombus"<<endl;
int x1, y1, x2, y2 = 0;
int count = 0;
coordinateLines();
while(1)
{
delay (0.0001);
if (ismouseclick(WM_LBUTTONDOWN))
{
getmouseclick(WM_LBUTTONDOWN, x1, y1);
cout<<"A("<<setXMachine2User(x1)<<","<<setYMachine2User(y1)<<")\n";
putpixel(x1,y1,color);
count++;
}
if (count == 1)
{
if (ismouseclick(WM_LBUTTONUP))
{
getmouseclick(WM_LBUTTONUP, x2, y2);
cout<<"B("<<setXMachine2User(x2)<<","<<setYMachine2User(y2)<<")\n";
drawRhombus(x1, y1, x2, y2);
cout<<"-------------------------------"<<endl;
count++;
}
}
if (count == 2)
{
count = 0;
}
}
}
First question, I have tried to calculate the expression, di+1=di+2*Δy−2*Δx(yi+1−yi) for the four quadrants. Irrespective of the quadrant, the expression was found to be the same, including signs.
Am I right, or, there has been some mistakes in my calculations (hence, I am wrong)?
Second question, if this expression is only applicable for the first octet, how can I apply this to other octets? To me, there is no way to determine which octet I am working on. Coz, the value of m always represent two opposite octets. For example, if 0<m<1, it represents 1st and 5th octet. Right?
Thirdly, how can we determine the initial/starting value of di?
#include <iostream>
#include "utils.h"
void BresenhamLine(double x1, double y1, double x2, double y2, int color)
{
if(x1>x2 || y1>y2)
{
Swap(x1, x2);
Swap(y1, y2);
}
double x = x1;
double y = y1;
double dx = x2 - x1;
double dy = y2 - y1;
double dt = 2 * (dy - dx);
double ds = 2 * dy;
double d = 2*dy - dx;
PlotPixel(x, y, color);
if(dx>=dy)
{
while(x<=x2)
{
x++;
if(d<0)
{
d = d + ds;
}
else
{
y++;
d = d + dt;
}
PlotPixel(x, y, color);
}
}
else
{
while(y<=y2)
{
y++;
if(d<0)
{
x++;
d = d + dt;
}
else
{
d = d + ds;
}
PlotPixel(x, y, color);
}
}
}
int main()
{
int gm = DETECT;
int gd = DETECT;
initgraph(&gm, &gd, "");
double x1 = 0;
double y1 = 0;
double r = 50;
double x2 = 0;
double y2 = 0;
double signx = 0;
double signy = 0;
for(int theta=0 ; theta<=360 ; theta++)
{
x2 = r * cos(DegreeToRad((double) theta));
y2 = r * sin(DegreeToRad((double) theta));
x1 = 5 * cos(DegreeToRad((double) theta));
y1 = 5 * sin(DegreeToRad((double) theta));
BresenhamLine(x1, y1, x2, y2, YELLOW);
}
getch();
closegraph();
return 0;
}
The lines that go through 2nd and 4th quadrant are not showing up.
How to fix that with some minor changes in my code?
With this input: x1: 100 y1: -100 x2: -100 y2: 100
this logic:
if(x1>x2 || y1>y2)
{
Swap(x1, x2);
Swap(y1, y2);
}
fails.
This page is a good place to start. It shows code as well for 1 of the octants:
http://www.cs.helsinki.fi/group/goa/mallinnus/lines/bresenh.html
I think you need to swap the x if x1 > x2 and swap the y if y1 > y2 but not swap both if only 1 of those is true.
The external links section of the Wikipedia page contains several links to ready-made implementations that you can study.
Try this:
void BresenhamLine( double x1, double y1, double x2, double y2, int color )
{
const bool steep = (std::abs(y2 - y1) > std::abs(x2 - x1));
if(steep)
{
std::swap(x1, y1);
std::swap(x2, y2);
}
if(x1 > x2)
{
std::swap(x1, x2);
std::swap(y1, y2);
}
double dx = x2 - x1;
double dy = std::abs(y2 - y1);
double error = dx / 2;
int ystep = (y1 < y2) ? 1 : -1;
int y = (int)y1;
int maxX = (int)x2;
for(int x=(int)x1; x<maxX; x++)
{
if(steep)
{
PlotPixel(y, x, color);
}
else
{
PlotPixel(x, y, color);
}
error -= dy;
if(error < 0)
{
y += ystep;
error += dx;
}
}
}
I got this by slightly modifying the code here:http://rosettacode.org/wiki/Bitmap/Bresenham's_line_algorithm#C.2B.2B
#include <iostream>
#include <math.h>
using namespace std;
class circle
{
public:
circle();
circle(double radius);
double circlerad(void);
double area(void);
double circumference(void);
private:
double rad;
};
circle::circle()
{
double rad = 0;
}
circle::circle(double radius)
{
cout << radius << endl;
double rad = radius;
}
double circle::circlerad(void)
{
return rad;
}
double circle::area(void)
{
double res;
res = rad * rad * 3.14;
return res;
}
double circle::circumference(void)
{
return (double) rad * 2 * 3.14;
}
double radius(int x1, int y1, int x2, int y2)
{
double rad = 0;
double xm, ym;
xm = (double)(x1 + x2) / 2;
ym = (double)(y1 + y2) / 2;
rad = sqrt((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1));
return rad;
}
int main(void)
{
double rad;
int x1, y1;
int x2, y2;
cout << "x1 : ";
cin >> x1;
cout << "y1 : ";
cin >> y1;
cout << "x2 : ";
cin >> x2;
cout << "y2 : ";
cin >> y2;
*circle one((double)radius(x1, y1, x2, y2));*
cout << fixed << one.area() << endl;
//cout << fixed << one.circumference() << endl;
return 0;
}
this is my code to calculate circle area and circumference.
But the problem is that when I initialize circle one, regardless of radius(x1, y1, x2, y2) value,
it always initialize to -9.2559631349317831e+061.
You don't initialise circle::rad here (instead you use local variable rad):
circle::circle()
{
double rad = 0;
}
circle::circle(double radius)
{
cout << radius << endl;
double rad = radius;
}
you should do something like:
circle::circle(): rad(0)
{
}
circle::circle(double radius): rad(radius)
{
}
double rad declares a local variable, which is not the same as the class member rad. The simplest fix:
circle::circle()
{
rad = 0;
}
circle::circle(double radius)
{
cout << radius << endl;
rad = radius;
}