Recent

Author Topic: overloading operators in fpc?  (Read 1529 times)

alsor

  • New Member
  • *
  • Posts: 47
Re: overloading operators in fpc?
« Reply #30 on: March 23, 2021, 09:51:07 pm »
You want code?

this is a simple code with overloaded operators context:

struct TP3d
{
  double x,y,z;

  TP3d& set(double a, double b, double c) { x=a; y=b; z=c; return *this; }
    void add(const TP3d &r) { x += r.x; y += r.y; z += r.z; }
    void sub(const TP3d &r) { x -= r.x; y -= r.y; z -= r.z; }
    void sub(const TP3d &a, const TP3d &b) { x = a.x-b.x; y = a.y-b.y; z = a.z-b.z; }

    void mul(double m) { x *= m; y *= m; z *= m; }
    void addm(const TP3d &r, double m) { x += r.x*m; y += r.y*m; z += r.z*m; }
    void subm(const TP3d &r, double m) { x -= r.x*m; y -= r.y*m; z -= r.z*m; }
   void setm(const TP3d &r, double m) { x = r.x*m; y = r.y*m; z = r.z*m; }

    double len()  { return sqrt(x*x+y*y+z*z); }
   double norm() { return x*x+y*y+z*z; }
    void unit();

   double operator*(const TP3d &b) { return x*b.x + y*b.y + z*b.z; }
    TP3d& operator+=(const TP3d &b) { add(b); return *this; }
};


struct TP4d : public TP3d
{
  double s;
};

inline double sqr(double a) { return a*a; }

TP3d& transf(TP3d &a, TP3d mx[]);

inline double operator*(const TP3d &a, const TP3d &b)
{ return a.x*b.x + a.y*b.y + a.z*b.z; }

inline TP3d& add(TP3d &r, const TP3d &a, const TP3d &b)
{ r.x = a.x+b.x; r.y = a.y+b.y; r.z = a.z+b.z; return r; }

inline TP3d& addm(TP3d &r, const TP3d &a, const TP3d &b, double m)
{ r.x = a.x + b.x*m; r.y = a.y+b.y*m; r.z = a.z+b.z*m; return r; }

TP3d& cross(TP3d &a, TP3d &b, TP3d &c);
      

inline double msub(TP3d &a, TP3d &b, TP3d &c)
{ return a.x*(b.x-c.x) + a.y*(b.y-c.y) + a.z*(b.z-c.z); }

inline void ipol(TP3d &p, TP3d &q, double a, double b)
{
   p.x = p.x*a + q.x*b;
   p.y = p.y*a + q.y*b;
   p.z = p.z*a + q.z*b;
}   

inline void ipold(TP3d &r, TP3d &p, TP3d &q, double m[], double mL[])
{
   r.x = p.x*mL[0] + q.x*mL[1];
   r.y = p.y*mL[0] + q.y*mL[1];
   r.z = p.z*mL[0] + q.z*mL[1];
   ipol(p,q,m[0],m[1]);
}   

inline void ipol3(TP3d &p, TP3d &q, TP3d &r, double c[])
{
   p.x = p.x*c[2] + q.x*c[1] + r.x*c[0];
   p.y = p.y*c[2] + q.y*c[1] + r.y*c[0];
   p.z = p.z*c[2] + q.z*c[1] + r.z*c[0];
}   

//======== bezier

inline void ipolL(TP3d p[], double m)
{
  ipol(p[0],p[1],1-m,m); 
}   

inline void ipolQ(TP3d p[] double m) //

  ipolL(p,m);
  ipolL(p+1,m);
  ipolL(p,m);
}   

inline void ipolC(TP3d p[], double m)
{
  ipolQ(p,m); // p[0] = ...
  ipolQ(p+1,m); //
  ipolL(p,m);
}   


JanRoza

  • Hero Member
  • *****
  • Posts: 618
    • http://www.silentwings.nl
Re: overloading operators in fpc?
« Reply #31 on: March 23, 2021, 10:03:09 pm »
Fantastic, it is C code.
But this is a Pascal forum so I think you are in the wrong forum.
I thought you were the king of all Pascal programmers and would show us here what we all do wrong and how you can do it better.
What a disappointment!
OS: Windows 10 (64 bit) / Linux Mint (64 bit)
Laz: Lazarus 2.0.12 FPC 3.2.0
       CodeTyphon 7.4 FPC 3.3.1

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2587
    • havefunsoft.com
Re: overloading operators in fpc?
« Reply #32 on: March 23, 2021, 10:05:59 pm »
can You rewrite this code to the free-pascal?
Code: C  [Select][+][-]
  1. inline operator + (T3DPoint a, T3DPoint b) { return T3DPoint(a.x+a.x, a.y+b.y, a.z+b.z); }

Code: Pascal  [Select][+][-]
  1. type
  2.   T3dpoint = record
  3.     x,y,z: double;
  4.   end;
  5.  
  6. operator + (const p1, p2: T3dpoint): T3dpoint; inline;
  7. begin
  8.   with result do begin
  9.     x:=p1.x+p1.x;
  10.     y:=p1.y+p2.y;
  11.     z:=p1.z+p2.z;
  12.   end;
  13. end;
  14.  
  15. operator + (const p1 : T2dpoint; const p2: T2dpoint): T3dpoint; inline;
  16. begin
  17.   with result do begin
  18.     x:=p1.x+p1.x;
  19.     y:=p1.y+p2.y;
  20.     z:=p1.z;
  21.   end;
  22. end;
  23.  

It's quite odd to me. But the urge need of operator overloading for Points and Matrix always follow people who is doing 3d graphics.

Here's a sample of a game engine. Plenty of operator overloading used in different combinations.
« Last Edit: March 23, 2021, 10:14:44 pm by skalogryz »
Patron Cocoa Widgetset development https://www.patreon.com/skalogryz

MarkMLl

  • Hero Member
  • *****
  • Posts: 2494
Re: overloading operators in fpc?
« Reply #33 on: March 23, 2021, 10:07:32 pm »
this is a simple code with overloaded operators context:

And your question is...?

You've already been pointed at the relevant documentation, please refer to that before wasting any more bandwidth.

MarkML
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

alsor

  • New Member
  • *
  • Posts: 47
Re: overloading operators in fpc?
« Reply #34 on: March 23, 2021, 10:33:08 pm »

1. Somebody wants some code then i give it.

2. My quest is: what is the time in the east Singapore now?

engkin

  • Hero Member
  • *****
  • Posts: 2692
Re: overloading operators in fpc?
« Reply #35 on: March 23, 2021, 11:11:47 pm »
1. Somebody wants some code then i give it.

You are kidding, right? Nice one, am laughing. Now where is the code?

winni

  • Hero Member
  • *****
  • Posts: 2323
Re: overloading operators in fpc?
« Reply #36 on: March 23, 2021, 11:16:52 pm »
Hi!

He is only begging for answers - whatever the answer is.

To get rid of this troll don't answer him.

That is greatest punishment for this poor boy.

Winni

engkin

  • Hero Member
  • *****
  • Posts: 2692
Re: overloading operators in fpc?
« Reply #37 on: March 23, 2021, 11:31:47 pm »
We don't want to get rid of him, we want to build a golden statue for him. There will be two, one for him and the other for an equally.great and famous figure.

I should thank him. I managed to finish something boring today, and did not feel the time because of all the entertainment he provided. I am really eager to learn from his expertise. Anticipating he'll finally give us his GitHub link to see his great code in C, C++ and Pascal.  :)

 

TinyPortal © 2005-2018