|
Tarun Jha wrote: as in the above code i have converted class Polar to class Rectangle,
Did you test it?
Does it work?
I ask because I see neither the assignment operator
Rectangle& Rectangle::operator =(const Polar& polar) nor the ctor to create Rectangle instance from the Polar one... 
|
|
|
|
|
but i am not using any variables with reference to heap, so i thought it shouldn't be needed for ex:- new, *variable etc.
|
|
|
|
|

#include <iostream>
#include <cmath>
using namespace std;
class Polar;
class Rectangle
{
float length, bredth;
public:
Rectangle(){}
Rectangle(float ,float);
operator Polar();
int getLength(){return(length);}
int getBredth(){return(bredth);}
void putLength(float x){length = x;}
void putBredth(float y){bredth = y;}
void putData(){cout<<"Length : "<<length<<"\tBreadth : "<<bredth<<endl;}
~Rectangle(){}
};
class Polar{
float angle, rad;
public:
Polar(){}
Polar(float , float);
operator Rectangle();
int getAngle(){return(angle);}
int getRadius(){return(rad);}
void putAngle(float a){angle=a;}
void putRadius(float r){rad = r;}
void putData(){cout<<"Angle : "<<angle<<"\tRadius : "<<rad<<endl;}
~Polar(){}
};
Rectangle::Rectangle(float x, float y)
{
length = x;
bredth = y;
}
Rectangle::operator Polar()
{
Polar temp;
float a = atan2(bredth, length), r = sqrt(length*length + bredth*bredth);
temp.putAngle(a);
temp.putRadius(r);
return(temp);
}
Polar::Polar(float x, float y)
{
angle = x;
rad = y;
}
Polar::operator Rectangle()
{
Rectangle temp;
float l = rad*cos(angle), b =rad*sin(angle);
temp.putLength(l);
temp.putBredth(b);
return(temp);
}
int main()
{
float radians = 120 * M_PI / 180;
Polar p1(radians, 25);
p1.putData();
Rectangle r1 = p1;
r1.putData();
Polar p2 = r1;
p2.putData();
return 0;
}
|
|
|
|
|
thank you 
|
|
|
|
|
|
here ih the code below i have tried to add two matrix objects and assign it to the third object but it doesn't give expected result and the rather some garbage value.
include <iostream>
using namespace std;
class matrix{
int **p;
int d1, d2;
public:
matrix(int x=20, int y=20);
void get_element();
void put_element();
int checkAdd(matrix &);
matrix operator + (matrix &);
void operator = (matrix );
~matrix(){
for (int i = 0; i < d1; i++)
delete p[i];
delete p;
}
};
matrix temp;
void matrix::operator = (matrix obj){
this->d1 = obj.d1;
this->d2 = obj.d2;
for(int i=0; i<d1; i++)
for(int j=0; j<d2; j++)
this->p[i][j] = obj.p[i][j];
}
matrix matrix::operator+(matrix &obj){
if(checkAdd(obj)==0)
{
for(int i=0; i<obj.d1; i++)
for(int j=0; j<obj.d2; j++)
temp.p[i][j] = this->p[i][j] + obj.p[i][j];
return(temp) ;
}
cout<<"coloumn and rows of both matrix are not equal !"<<endl;
return (*this);
}
matrix ::matrix(int x, int y)
{
d1 = x;
d2 = y;
p = new int *[d1];
for(int i=0; i<d1; i++)
p[i] = new int [d2];
}
int matrix::checkAdd(matrix &obj){
if((this->d1 == obj.d1) && (this->d2 == obj.d2))
return 0;
else return 1;
}
void matrix::get_element(){
for(int i=0; i<d1; i++)
for(int j=0; j<d2; j++){
cout<<"m["<<i<<"]["<<j<<"] = ";
cin>>p[i][j];
}
}
void matrix::put_element(){
cout<<"\n\n";
for(int i=0; i<d1; i++){
for(int j=0; j<d2; j++){
cout<<p[i][j]<<"\t";
}
cout<<endl;
}
}
int main(){
matrix A(3, 2), B(3, 2), C;
cout<<"Enter matrix elements row by row \n\n";
A.get_element();
A.put_element();
B.get_element();
B.put_element();
C = A + B;
cout<<"\n\n";
C.put_element();
return 0;
}
thank you
|
|
|
|
|
What result did you expect and what did you get?
|
|
|
|
|
Quote: So basically i want to add 2 matrix object and assign the result to the third non-initiated matrix object, without changing the values of first 2 original matrix.
this is the result i am getting.
Quote: Enter matrix elements row by row
m[0][0] = 1
m[0][1] = 2
m[1][0] = 3
m[1][1] = 4
m[2][0] = 5
m[2][1] = 6
1 2
3 4
5 6
m[0][0] = 7
m[0][1] = 8
m[1][0] = 9
m[1][1] = 10
m[2][0] = 11
m[2][1] = 12
7 8
9 10
11 12
8 10 47120720 0 0 0 0 0 0 0 50331651 41272 47127056 0 47120720 0 0 2396 136 0
12 14 47120720 0 1550742898 1148219457 1549890657 1633906508 1700027500
1426092141 1146242387 1229016399 1162100046 1330924371 1429482832 844253517 1398079566 1329877573 1313423693 1095717471
16 18 47120720 0 1162690894 1918981181 1426091637 1347568979 1229344594
1128088908 1934974010 1551069797 1970430292 1398145134 1127232561 1414417743 1397509967 1547322173 1735357008 544039282
47120720 0 47127424 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0
47120720 0 47127424 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0
47120720 0 47127424 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0
47120720 0 47127424 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0
47120720 0 47127424 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0
47120720 0 47127424 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0
47120720 0 47127424 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0
47120720 0 47127424 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0
47120720 0 47127424 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0
47120720 0 47127424 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0
47120720 0 47127424 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0
47120720 0 47127424 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0
47120720 0 47127424 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0
47120720 0 47127424 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
i want a result:
8 10
12 14
16 18
|
|
|
|
|
You already posted this exact question in the ATL/STL forum, and I gave you a suggestion. If you google for "operator overload C++" you will find plenty of samples.
|
|
|
|
|
 i was told that these type of questions are to be posted here.
and i did the program..
#include <iostream>
using namespace std;
class matrix{
int **p;
int d1, d2;
int id=0;
public:
matrix(int x=20, int y=20);
int getD_1(){return (d1);}
int getD_2(){return (d2);}
void get_element();
void put_element();
friend int checkAdd(matrix &, matrix &);
friend int checkMul(matrix &, matrix &);
friend matrix operator+(matrix &, matrix &);
friend matrix operator*(matrix &, matrix &);
void operator = (matrix );
~matrix(){
for (int i = 0; i < d1; i++)
delete p[i];
delete p;
}
};
void matrix::operator = (matrix obj){
this->d1 = obj.d1;
this->d2 = obj.d2;
this->id = obj.id;
for(int i=0; i<d1; i++)
for(int j=0; j<d2; j++)
this->p[i][j] = obj.p[i][j];
}
matrix operator+(matrix &a, matrix &b){
matrix temp;
temp.d1 = a.d1;
temp.d2 = a.d2;
temp.id = 1;
if(checkAdd(a, b) == 0){
for(int i=0; i<temp.d1; i++)
for(int j=0; j<temp.d2; j++)
temp.p[i][j] = a.p[i][j] + b.p[i][j] ;
return(temp);
}
else {
cout<<"Matrix addition not possible as rows and coloumn's of given 2 matrices are not equal"<<endl;
return(a);
}
}
matrix operator*(matrix &a, matrix &b){
matrix temp;
temp.d1 = a.d1;
temp.d2 = b.d2;
temp.id = 2;
if(checkMul(a, b) == 0){
int sum=0;
for (int c = 0; c < a.d1; c++) {
for (int d = 0; d < b.d2; d++) {
for (int k = 0; k <b.d1; k++) {
sum = sum + a.p[c][k]*b.p[k][d];
}
temp.p[c][d] = sum;
sum = 0;
}
}
return (temp);
}
else {
cout<<"Matrix Multiplication is not possible as coloumn of 1st Matrix != Row of 2nd Matrix "<<endl;
return(a);
}
}
matrix ::matrix(int x, int y){
d1 = x;
d2 = y;
p = new int *[d1];
for(int i=0; i<d1; i++)
p[i] = new int [d2];
}
int checkAdd(matrix &a, matrix &b){
if(a.getD_1() == b.getD_1() && a.getD_2() == b.getD_2())
return 0;
else return 1;
}
int checkMul(matrix &a, matrix &b){
if(a.getD_2() == b.getD_1())
return 0;
else return 1;
}
void matrix::get_element(){
for(int i=0; i<d1; i++)
for(int j=0; j<d2; j++){
cout<<"m["<<(i+1)<<"]["<<(j+1)<<"] = ";
cin>>p[i][j];
}
}
void matrix::put_element(){
if(id == 1) cout<<"\nThe result of addition of 2 matrices is :"<<endl;
else if(id==2) cout<<"\nThe result of product of 2 matrices is:"<<endl;
for(int i=0; i<d1; i++){
for(int j=0; j<d2; j++){
cout<<p[i][j]<<"\t";
}
cout<<endl;
}
cout<<"\n";
}
int main(){
matrix A(3, 2), B(2, 4), C;
cout<<"Enter matrix elements row by row \n\n";
A.get_element();
A.put_element();
B.get_element();
B.put_element();
C = A * B;
C.showId();
C.put_element();
C = A + B;
C.showId();
C.put_element();
return 0;
}
and thank you for the advice
|
|
|
|
|
Forget about matrix calculations. You need to go and study operator overloading and get that working on a basic class first. Once you understand how to do it correctly so you get no errors, then you can move on to the matrix functions.
|
|
|
|
|
is there anything wrong with the program i have posted before ?
|
|
|
|
|
I don't know; you have to do your own testing. But I find one of the easiest way to debug issues is to create a small test program that just focused on the area that appears not to work. So a small program with a very basic class, that just tests the operator+ overload, would be much easier to understand.
|
|
|
|
|
ok...
thanks for the advice
|
|
|
|
|
Broken 5V supply to LCD!
I should have know better since the LCD did not react to power-up - reset and show 5/7 characters - but the power LED was on on the i2c module itself !
Learned few Linux tools on how to troubleshoot i2c, so no big loss.
Now I can proceed with implementing ioctl.
I would like to discuss implementation of Linux ioctl function.
There are few reasons I am posting my question here and not on Linux forum. However, if you feel it is inappropriate you have two options - ignore my request ( preferred to sermon ) or flag it to be removed.
I am basically aware WHAT ioctl does, and have been thru few documents ( Linux Device drivers, CodeProject driver tutorials etc.)
I am trying to implement ioctl "driver" to output (Raspberry Pi) to I2C LCD.
The problems I cannot get straight is how to configure I2C speed (using C++, not Linux commands ) and implement ACK read from slave I2C device.
I understand I2C protocol, but all of the sample codes I have read so far are "limited" to open ioctl file and "write " to I2C slave address.
I like to see or make my own code to implement I2C "start /stop " and read ACK/ NACK from slave.
Any to the point help will as always be appreciated.
Thanks
-- modified 7-Apr-18 15:35pm.
|
|
|
|
|
You have two options:
- Use the existing I2C support on the Raspberry Pi with the I2C GPIO pins
- Implement your own I2C support on general use GPIO pins
The I2C speed is defined by the master by generating the clock. A device might use "clock stretching" to slow down (Clock, Stretching, Arbitration - I2C Bus[^]).
So implementations are not time critical. They can use a predefined (max.) clock and have not to care about being slowed down by other tasks on the system.
I have implemented I2C in assembler on micro controllers. Implementing it in C is not a problem but be aware that it might be a frustrating tasks because it can't be simply debugged. I highly recommend to use a logic analyser or at least a storage oscilloscope to check the signals on the bus.
Quote: all of the sample codes I have read so far are "limited" to open ioctl file and "write " to I2C slave address. With device drivers use i2c_transfer() . Have a look at the source of any Linux driver for a I2C device like rtc-pcf8563.c
[^].
When using ioctl() on the I2C device just use read() :
int fd = open("/dev/i2c-1", O_RDWR);
ioctl(fd, I2C_SLAVE, addr);
read(fd, buffer, length); Or use ioctl(I2C_RDWR) or ioctl(I2C_SMBUS) for reading which both does not require the I2C_SLAVE[_FORCE] . In any case check first if the used ioctl() call is supported.
|
|
|
|
|
The speed is configured outside the program or on the library you are using on the Pi
Edit /boot/config.txt if it doesn't exist create it
add the line or edit the line with your baudrate
dtoverlay=i2c-speed,i2c1_baudrate=400000
i2c1 may also be disabled by default I can't remember if so you need
dtparam=i2c_vc=on
In the userland repository there is also a direct header and library
bcm2835: C library for Broadcom BCM 2835 as used in Raspberry Pi[^]
and a forum
bcm2835 : Google Groups[^]
Standard use
#include <bcm2835.h>
int main(int argc, char **argv)
{
char buf[1];
if (!bcm2835_init())return 1;
bcm2835_i2c_begin();
bcm2835_i2c_setSlaveAddress(0x20);
bcm2835_i2c_set_baudrate(10000);
while(1)
{
buf[0] = 0xEF;
bcm2835_i2c_write(buf,1);
bcm2835_delay(500);
buf[0] = 0xFF;
bcm2835_i2c_write(buf,1);
bcm2835_delay(500);
}
bcm2835_i2c_end();
bcm2835_close();
return 0;
}
In vino veritas
modified 26-Mar-18 23:06pm.
|
|
|
|
|
Thanks for replies.
I am currently "on the road" and will reply when I get home.
In short
I did try "just GPIO" , it worked to the point, but it got too convoluted.
Hence I am after using ioctl.
I'll take a look at the bcm2835.h, hopefully I can use it for bcm2837 chip used in RPi 3B.
I think my main concern is still to be able to "see" the code following / running the I2C spec - mainly changing hardware pins directions and reading START /STOP and ACK/NACK back. Of course I am verifying the actual LCD and sometime even using scope.
Thanks for all your comments, appreciate that.
-- modified 5-Apr-18 12:42pm.
|
|
|
|
|
I converted VS2008 solution (sln) to VS2017. Visual Studio is up-to-date - version 15.6.4.
My solution contains tens of C++ projects - executables, dll's and lib's. Now I have a weird issue - dll's cannot be debugged in Debug configuration.
In Release configuration it works - breakpoints can be used.
But in Debug configuration all the breakpoints are disabled and there is a hint - "The breakpoint will not currently be hit. No symbols have been loaded for this document."
When I try to load symbols (pdb) manually I'm getting a message "A matching symbol file was not found in this folder."
In DLL project settings (vcxproj file) GenerateDebugInformation is true.
I'm a bit confused why generated pdb file cannot be used by Visual Studio.
|
|
|
|
|
The chances are that your DLL needs to be rebuilt.
|
|
|
|
|
Have you converted the solution in-place (with existing output files from old VS 2008 builds)?
Then try to delete all output files (especially old ones which can be identified by the time stamp).
|
|
|
|
|
Hi,
The 'Debug' build is just a text label... in other words it sounds like your DLL is being compiled with the symbols stripped out. There is nothing stopping someone from configuring 'Debug' exactly as a 'Release' build.
Try using DUMPBIN /SYMBOLS[^] on your DLL to check if the symbol tables exists.
Don't forget that you can PIPE the output into a text file:
DUMPBIN /SYMBOLS YourDLL.DLL > symbols.txt
Best Wishes,
-David Delaune
|
|
|
|
|
Hello Everybody,
I am using VC++ and OpenGL for one application.
I am new to OpenGL. I can able to load different objects and also able to create vertices. After that, I was able to select and pick those using glReadPixels and gluUnProject.
Now, I need to select and pick lines (edges of the triangles). I searched in google and not able to find a proper result.
Can anybody give me ideas how to implement this?
Thanks in Advance.
Regards,
Gopi.
|
|
|
|
|
Find the line in your list that is closest the point or at least less than some tolerance value
A number of Contributed implementations of the formula here plus an explaination of the mathematics
Point, Line, Plane[^]
Implementation would look something like this
#define GLfloat float // No sure if you want floats or double you can change here
GLfloat GLDistance2D (GLfloat x1, GLfloat y1,
GLfloat x2, GLfloat y2)
{
GLfloat dx, dy;
dx = x2 - x1;
dy = y2 - y1;
if ( dx < -FLT_EPSILON || dx > FLT_EPSILON ||
dy < -FLT_EPSILON || dy > FLT_EPSILON )
{
return sqrtf(dx * dx + dy * dy);
}
return (GLfloat) 0.0;
}
GLfloat GLDistanceToLine2D (GLfloat px, GLfloat py,
GLfloat x1, GLfloat y1,
GLfloat x2, GLfloat y2,
bool linesegment,
GLfloat* cpx, GLfloat* cpy)
{
GLfloat u;
GLfloat dx = x2 - x1;
GLfloat dy = y2 - y1;
GLfloat SqLineMag = dx * dx + dy * dy;
if (SqLineMag > -FLT_EPSILON && SqLineMag < FLT_EPSILON) {
if (linesegment) return GLDistance2D(px, py, x1, y1);
else return (GLfloat)-1.0;
}
u = ((px - x1)* dx + (py - y1)* dy) / SqLineMag;
if ((linesegment) && ((u < FLT_EPSILON) || (u > (GLfloat)1.0)))
{
GLfloat ix, iy;
ix = GLDistance2D(px, py, x1, y1);
iy = GLDistance2D(px, py, x2, y2);
if (ix < iy) {
if (cpx) (*cpx) = x1;
if (cpy) (*cpy) = y1;
return(ix);
}
else {
if (cpx) (*cpx) = x2;
if (cpy) (*cpy) = y2;
return(iy);
}
}
else {
GLfloat ix, iy;
ix = x1 + (u * dx);
if (cpx) (*cpx) = ix;
iy = y1 + (u * dy);
if (cpy) (*cpy) = iy;
return GLDistance2D(px, py, ix, iy);
}
}
Run a test .. it should return ix,iy = (200,100) dist = 100 ... plot it you will see why.
GLfloat ix, iy;
GLfloat dist = GLDistanceToLine2D(100, 100, 200, 0, 200, 200, true, &ix, &iy);
In vino veritas
modified 23-Mar-18 2:36am.
|
|
|
|
|
Thanks Leon for your quick reply.
Will give it a try and update you.
Regards,
Gopi.
|
|
|
|
|