Chat GPT 4o Code
Re: Chat GPT 4o Code
I think this forum still beats chatgpt!
But, while hoomans are sleeping, AI is learning.
But, while hoomans are sleeping, AI is learning.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Re: Chat GPT 4o Code
And takes over world dominationskywalk wrote: Sun May 19, 2024 12:16 pm I think this forum still beats chatgpt!
But, while hoomans are sleeping, AI is learning.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Re: Chat GPT 4o Code
So basically.. Same problems 3.5 and 4 had....
Phantom functions
Non-existent libraries
Broken base syntax
By the way it does the same with Python and Rust and it's way better at those... The fact every time someone brings this up at least one person acts like experienced coders have been replaced and you don't have to pay intelligent people for skilled-work anymore is hilarious...
BTW I have full unmetered access to all OpenAI models, prompts, and API.. It's how I know all the models, even third party, have absolute garbage code-generation; even for Python... That's not even touching algorithm and parser stuff too; just simple introduction-level code...
Phantom functions
Non-existent libraries
Broken base syntax
By the way it does the same with Python and Rust and it's way better at those... The fact every time someone brings this up at least one person acts like experienced coders have been replaced and you don't have to pay intelligent people for skilled-work anymore is hilarious...
BTW I have full unmetered access to all OpenAI models, prompts, and API.. It's how I know all the models, even third party, have absolute garbage code-generation; even for Python... That's not even touching algorithm and parser stuff too; just simple introduction-level code...
Last edited by tj1010 on Sun May 19, 2024 1:15 pm, edited 1 time in total.
Re: Chat GPT 4o Code
Just for the record: that's NOT what I'm claiming, or have ever claimed.tj1010 wrote: Sun May 19, 2024 1:12 pmevery time someone brings this up at least one person acts like experienced coders have been replaced and you don't have to pay intelligent people for skilled-work anymore
Re: Chat GPT 4o Code
Would be a good idea to give this forum to an AI and let it to learn from
Re: Chat GPT 4o Code
I think they are good for generating forms etc. and filling standard codes in. I generated a LoginRequester on an other experimental site, specialized on code generation AI, but they blocked purebasic now after I probably tried to often or too hard, causing artificial headaches to inexistent personalities
Except that the AI invented some commands like CreateButton or so, the parameters and most of the code was 90% correct; even the flags were right which made me ask why it would invent new names for the the creation functions. It looked nice from the layout/design and the source was a lot like the output of the form designer, so I have corrected & added functionality to it. But I don't have stored the original source code.
I think programming should be where the AI's could have the greatest potential, even tho I think it's more of use for "templates" or so; perhaps with creating specific own "templates" for it to use, the generation of code could be more tailored to what is wanted and made to be less error prone.
What I find interesting is the tendency of seemingly optimizing the code over the attempts, even if it remains faulty in parts.
But this may also be due to the user's input, and the possibility of the AI to use preceding requests as a base for it's evaluations.

I think programming should be where the AI's could have the greatest potential, even tho I think it's more of use for "templates" or so; perhaps with creating specific own "templates" for it to use, the generation of code could be more tailored to what is wanted and made to be less error prone.
What I find interesting is the tendency of seemingly optimizing the code over the attempts, even if it remains faulty in parts.

But this may also be due to the user's input, and the possibility of the AI to use preceding requests as a base for it's evaluations.
Re: Chat GPT 4o Code
If the underlying language is tightly defined, AI will eventually conquer it.
SQL is a good candidate.
Your tax form is easy.
Self driving automobile is the other extreme.
Wealth creation is next. "Alexa, here are my bank passwords. Make my balance grow with the stock market."
SQL is a good candidate.
Your tax form is easy.
Self driving automobile is the other extreme.
Wealth creation is next. "Alexa, here are my bank passwords. Make my balance grow with the stock market."
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Re: Chat GPT 4o Code
We are talking about how experienced programmers can use it as a tool and how it is improving. Not a replacement for programmers. It is pretty easy for an experienced programmer to go through what it writes and fix it to their standards. Even could be quite educational for someone that doesn't know how to program it would be enough to get them started. I was just surprised how well it could actually write in purebasic, being as it is not a super well known langauge.tj1010 wrote: Sun May 19, 2024 1:12 pm So basically.. Same problems 3.5 and 4 had....
Phantom functions
Non-existent libraries
Broken base syntax
By the way it does the same with Python and Rust and it's way better at those... The fact every time someone brings this up at least one person acts like experienced coders have been replaced and you don't have to pay intelligent people for skilled-work anymore is hilarious...
BTW I have full unmetered access to all OpenAI models, prompts, and API.. It's how I know all the models, even third party, have absolute garbage code-generation; even for Python... That's not even touching algorithm and parser stuff too; just simple introduction-level code...
Seems to be people are having a lot of negative feelings about this subject lol. We know it is not perfect and can really get some simple things wrong even. The improvements are the impressive part and using it properly as a tool can bring up productivity for everyone.
Re: Chat GPT 4o Code
It breaks on base syntax stuff like parenthesis where none are required and weird non existing core library functions that break the whole code flow. It's not really about picking what you want to use it for; if it can't do anything without heavily mangling code flow...benubi wrote: Sun May 19, 2024 2:43 pm I think they are good for generating forms etc. and filling standard codes in. I generated a LoginRequester on an other experimental site, specialized on code generation AI, but they blocked purebasic now after I probably tried to often or too hard, causing artificial headaches to inexistent personalitiesExcept that the AI invented some commands like CreateButton or so, the parameters and most of the code was 90% correct; even the flags were right which made me ask why it would invent new names for the the creation functions. It looked nice from the layout/design and the source was a lot like the output of the form designer, so I have corrected & added functionality to it. But I don't have stored the original source code.
I think programming should be where the AI's could have the greatest potential, even tho I think it's more of use for "templates" or so; perhaps with creating specific own "templates" for it to use, the generation of code could be more tailored to what is wanted and made to be less error prone.
What I find interesting is the tendency of seemingly optimizing the code over the attempts, even if it remains faulty in parts.![]()
But this may also be due to the user's input, and the possibility of the AI to use preceding requests as a base for it's evaluations.
If anything it's useful for vague hints at API usage and algorithms. This includes for Python which is what it does best, and also includes all the third party models like Code CoPilot, Grimoire, AutoExpert, Code Guru etc..
Lot's of hype for something that basically slows down production for a solo programmer; of course, just speaking as someone who has published finished products and tools for decades as a solo.. If you get in to debugging and code&binary analysis any form of supportive argument immediately falls flat, since it can't even fake doing either..
By all means.. Don't let my rambling slow anyone down from posting whole code-base that any of these models have generated that runs

Re: Chat GPT 4o Code
There are projects to make """AI""" freely available, like
https://github.com/xtekky/gpt4free
(...)
…anyway IMHO """AI""" is just kind of a "(often biased) big database", so………
https://github.com/xtekky/gpt4free
(...)
…anyway IMHO """AI""" is just kind of a "(often biased) big database", so………
Re: Chat GPT 4o Code
Prompt:
Purebasic code for a program with a full working gui that appends two files a jpg image and a rar archive. The rar archive file is appended to the jpg image file and saved as a file named output.jpg
Response:
Certainly! Below is a PureBasic code example to create a simple GUI application that appends a JPEG image and a RAR archive file into a single output file named `output.jpg`.
### Explanation:
1. **OpenWindow()**: Creates the main window with specified dimensions and properties.
2. **TextGadget()**: Displays static text labels.
3. **ButtonGadget()**: Creates buttons for user interactions.
4. **OpenFileRequester()**: Opens file dialog for selecting JPG and RAR files.
5. **CreateFile()**, **ReadFile()**, **WriteData()**, and **CloseFile()**: Handle file operations for reading the input files and writing to the output file.
6. **MessageRequester()**: Displays message dialogs for success or error notifications.
### Usage:
1. Run the program.
2. Click "Browse..." to select a JPG file and a RAR file.
3. Click "Append Files" to append the RAR file to the JPG file and save the result as `output.jpg`.
This code is a basic implementation and can be further improved with additional error handling and user interface enhancements as needed.
Purebasic code for a program with a full working gui that appends two files a jpg image and a rar archive. The rar archive file is appended to the jpg image file and saved as a file named output.jpg
Response:
Certainly! Below is a PureBasic code example to create a simple GUI application that appends a JPEG image and a RAR archive file into a single output file named `output.jpg`.
Code: Select all
If OpenWindow(0, 100, 100, 400, 150, "Append JPG and RAR", #PB_Window_SystemMenu | #PB_Window_TitleBar | #PB_Window_ScreenCentered)
TextGadget(1, 10, 10, 100, 25, "Select JPG File:")
ButtonGadget(2, 120, 10, 100, 25, "Browse...")
TextGadget(3, 230, 10, 150, 25, "")
TextGadget(4, 10, 50, 100, 25, "Select RAR File:")
ButtonGadget(5, 120, 50, 100, 25, "Browse...")
TextGadget(6, 230, 50, 150, 25, "")
ButtonGadget(7, 150, 90, 100, 25, "Append Files")
Global jpgFile.s
Global rarFile.s
Repeat
Event = WaitWindowEvent()
Select Event
Case #PB_Event_Gadget
Select EventGadget()
Case 2 ; Browse for JPG
jpgFile = OpenFileRequester("Select a JPG file", "", "JPEG Images|*.jpg;*.jpeg", 0)
If jpgFile
SetGadgetText(3, GetFilePart(jpgFile))
EndIf
Case 5 ; Browse for RAR
rarFile = OpenFileRequester("Select a RAR file", "", "RAR Archives|*.rar", 0)
If rarFile
SetGadgetText(6, GetFilePart(rarFile))
EndIf
Case 7 ; Append Files
If jpgFile And rarFile
If CreateFile(0, "output.jpg")
If ReadFile(1, jpgFile) And ReadFile(2, rarFile)
WriteData(0, ReadData(1, Lof(1)))
WriteData(0, ReadData(2, Lof(2)))
CloseFile(1)
CloseFile(2)
MessageRequester("Success", "Files have been appended successfully!", #PB_MessageRequester_Ok)
Else
MessageRequester("Error", "Failed to read files.", #PB_MessageRequester_Ok)
EndIf
CloseFile(0)
Else
MessageRequester("Error", "Failed to create output file.", #PB_MessageRequester_Ok)
EndIf
Else
MessageRequester("Error", "Please select both JPG and RAR files.", #PB_MessageRequester_Ok)
EndIf
EndSelect
EndSelect
Until Event = #PB_Event_CloseWindow
EndIf
End
1. **OpenWindow()**: Creates the main window with specified dimensions and properties.
2. **TextGadget()**: Displays static text labels.
3. **ButtonGadget()**: Creates buttons for user interactions.
4. **OpenFileRequester()**: Opens file dialog for selecting JPG and RAR files.
5. **CreateFile()**, **ReadFile()**, **WriteData()**, and **CloseFile()**: Handle file operations for reading the input files and writing to the output file.
6. **MessageRequester()**: Displays message dialogs for success or error notifications.
### Usage:
1. Run the program.
2. Click "Browse..." to select a JPG file and a RAR file.
3. Click "Append Files" to append the RAR file to the JPG file and save the result as `output.jpg`.
This code is a basic implementation and can be further improved with additional error handling and user interface enhancements as needed.
HP Z800 Workstation
CPU : Dual Xeon 5690 3.46GHz
RAM : 96GB RAM ( 8GB x 12 )
PSU : 1100W
GPU : NVIDIA RTX 3050 8GB
STORAGE : 9TB
(4) 2TB Seagate IronWolf Pro HDD
(1) 1TB Samsung 870 EVO SSD
CPU : Dual Xeon 5690 3.46GHz
RAM : 96GB RAM ( 8GB x 12 )
PSU : 1100W
GPU : NVIDIA RTX 3050 8GB
STORAGE : 9TB
(4) 2TB Seagate IronWolf Pro HDD
(1) 1TB Samsung 870 EVO SSD
Re: Chat GPT 4o Code
If you start the prompt assigning a role, you (almost always) get better results.
Something like: "You are an expert PureBasic programmer with more than 10 years of experience.."
Something like: "You are an expert PureBasic programmer with more than 10 years of experience.."
Re: Chat GPT 4o Code
WTF hooman!? CHECKMATE!Caronte3D wrote: Sun May 26, 2024 5:31 pm If you start the prompt assigning a role, you (almost always) get better results.
Something like: "You are an expert PureBasic programmer with more than 10 years of experience.."
Re: Chat GPT 4o Code
Mohawk70 wrote: Sat May 25, 2024 10:52 pmPurebasic code for a program with a full working gui that appends two files a jpg image and a rar archive. The rar archive file is appended to the jpg image file and saved as a file named output.jpg
Code: Select all
; PureBasic 6.0 example code
; Define constants for GUI elements
#Window_Main = 0
#Button_JPG = 1
#Button_RAR = 2
#Button_Save = 3
#Text_JPG = 4
#Text_RAR = 5
Global jpgFile$, rarFile$
; Callback procedure for the GUI
Procedure MainWindowCallback(Event)
Select Event
Case #PB_Event_Gadget
Select EventGadget()
Case #Button_JPG
jpgFile$ = OpenFileRequester("Select JPG File", "", "*.jpg|*.jpg", 0)
SetGadgetText(#Text_JPG, jpgFile$)
Case #Button_RAR
rarFile$ = OpenFileRequester("Select RAR File", "", "*.rar|*.rar", 0)
SetGadgetText(#Text_RAR, rarFile$)
Case #Button_Save
If jpgFile$ <> "" And rarFile$ <> ""
outputFile$ = SaveFileRequester("Save Output File", "", "*.jpg|*.jpg", 0)
If outputFile$ <> ""
If CreateFile(0, outputFile$)
If ReadFile(1, jpgFile$)
While Not Eof(1)
WriteData(0, ReadData(1, 4096), 4096)
Wend
CloseFile(1)
EndIf
If ReadFile(2, rarFile$)
While Not Eof(2)
WriteData(0, ReadData(2, 4096), 4096)
Wend
CloseFile(2)
EndIf
CloseFile(0)
MessageRequester("Success", "Files successfully appended!", #PB_MessageRequester_Ok)
EndIf
EndIf
Else
MessageRequester("Error", "Please select both JPG and RAR files.", #PB_MessageRequester_Ok)
EndIf
EndSelect
EndSelect
EndProcedure
; Create main window
If OpenWindow(#Window_Main, 100, 100, 400, 200, "JPG and RAR Appender", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
ButtonGadget(#Button_JPG, 20, 20, 100, 30, "Select JPG")
ButtonGadget(#Button_RAR, 20, 70, 100, 30, "Select RAR")
ButtonGadget(#Button_Save, 20, 120, 100, 30, "Save Output")
StringGadget(#Text_JPG, 130, 20, 240, 30, "")
StringGadget(#Text_RAR, 130, 70, 240, 30, "")
SetWindowCallback(@MainWindowCallback())
; Main event loop
Repeat
Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow
EndIf
End
MY prompt:[09:52:48] [COMPILER] Line 33: ReadData(): Incorrect number of parameters.
[09:55:30] [COMPILER] Line 40: ReadData(): Incorrect number of parameters.
[09:56:02] Waiting for executable to start...
[09:56:03] Executable type: Windows - x64 (64bit, Unicode)
[09:56:03] Executable started.
[09:56:03] [ERROR] Line: 65
[09:56:03] [ERROR] SetWindowCallback(): The Procedure specified in '@ProcedureName()' does not have the required argument or returntype.
But surely you jest
I apologize if my previous response didn't meet your expectations. Here's a more refined PureBasic code example that ensures the GUI functionality and the file operations are properly handled:
```purebasic
; PureBasic 6.0 example code
; Define constants for GUI elements
#Window_Main = 0
#Button_JPG = 1
#Button_RAR = 2
#Button_Save = 3
#Text_JPG = 4
#Text_RAR = 5
Global jpgFile$, rarFile$
; Procedure to append two files
Procedure AppendFiles(jpgFile$, rarFile$, outputFile$)
If CreateFile(0, outputFile$)
If ReadFile(1, jpgFile$)
While Not Eof(1)
buffer$ = Space(4096)
readBytes = ReadData(1, @buffer$, 4096)
WriteData(0, @buffer$, readBytes)
Wend
CloseFile(1)
EndIf
If ReadFile(2, rarFile$)
While Not Eof(2)
buffer$ = Space(4096)
readBytes = ReadData(2, @buffer$, 4096)
WriteData(0, @buffer$, readBytes)
Wend
CloseFile(2)
EndIf
CloseFile(0)
ProcedureReturn #True
Else
ProcedureReturn #False
EndIf
EndProcedure
; Callback procedure for the GUI
Procedure MainWindowCallback(Event)
Select Event
Case #PB_Event_Gadget
Select EventGadget()
Case #Button_JPG
jpgFile$ = OpenFileRequester("Select JPG File", "", "*.jpg|*.jpg", 0)
SetGadgetText(#Text_JPG, jpgFile$)
Case #Button_RAR
rarFile$ = OpenFileRequester("Select RAR File", "", "*.rar|*.rar", 0)
SetGadgetText(#Text_RAR, rarFile$)
Case #Button_Save
If jpgFile$ <> "" And rarFile$ <> ""
outputFile$ = SaveFileRequester("Save Output File", "", "*.jpg|*.jpg", 0)
If outputFile$ <> ""
If AppendFiles(jpgFile$, rarFile$, outputFile$)
MessageRequester("Success", "Files successfully appended!", #PB_MessageRequester_Ok)
Else
MessageRequester("Error", "Failed to save the output file.", #PB_MessageRequester_Ok)
EndIf
EndIf
Else
MessageRequester("Error", "Please select both JPG and RAR files.", #PB_MessageRequester_Ok)
EndIf
EndSelect
EndSelect
EndProcedure
; Create main window
If OpenWindow(#Window_Main, 100, 100, 400, 200, "JPG and RAR Appender", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
ButtonGadget(#Button_JPG, 20, 20, 100, 30, "Select JPG")
ButtonGadget(#Button_RAR, 20, 70, 100, 30, "Select RAR")
ButtonGadget(#Button_Save, 20, 120, 100, 30, "Save Output")
StringGadget(#Text_JPG, 130, 20, 240, 30, "")
StringGadget(#Text_RAR, 130, 70, 240, 30, "")
SetWindowCallback(@MainWindowCallback())
; Main event loop
Repeat
Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow
EndIf
End
```
### Explanation:
1. **AppendFiles Procedure**: This procedure handles the file reading and writing operations. It reads the JPG and RAR files in chunks of 4096 bytes and writes them to the output file.
2. **MainWindowCallback Procedure**: This handles the events for the GUI elements, including selecting files and saving the output.
3. **GUI Creation**: The GUI consists of buttons to select JPG and RAR files, and to save the combined file, along with text fields to display the selected file paths.
4. **Main Event Loop**: This loop keeps the window open and responsive to user interactions.
This code should provide a fully working GUI application to append a JPG image and a RAR archive, and save the result as a new JPG file.
Consider me corrected XD[10:08:05] Waiting for executable to start...
[10:08:05] Executable type: Windows - x64 (64bit, Unicode)
[10:08:05] Executable started.
[10:08:05] [ERROR] Line: 80
[10:08:05] [ERROR] SetWindowCallback(): The Procedure specified in '@ProcedureName()' does not have the required argument or returntype.
[10:08:08] The Program was killed.
[10:08:19] Waiting for executable to start...
[10:08:19] Executable type: Windows - x64 (64bit, Unicode)
[10:08:19] Executable started.
[10:08:24] The Program execution has finished.
- StarBootics
- Addict
- Posts: 1006
- Joined: Sun Jul 07, 2013 11:35 am
- Location: Canada
Re: Chat GPT 4o Code
My latest experience with Chat GPT last week I was searching for a solution for cubic interpolation spline. I have started to ask if this AI was aware about how to solve this problem using a tri-diagonal matrix. He replied that it's not the conventional way to solve this problem. Then I have supplied this as a prompt :
And then I have ask for a solution programmed in C (The solution can be translated in PureBasic afterward) and this is the solution the AI came up with :
As you can see, the solution, even if it work fine, you still have work to do even if you are programming in C.
Best regards
StarBootics
Solution for a natural parametric cubic interpolation spline.
So let's have some 3D control points for an 'open' spline.
P0 = [0, 0, 0]
P1 = [15, 9, 4]
P2 = [19, 25, -20]
P3 = [34, 15, -13]
P4 = [26, -5, 2]
P5 = [20, -10, -9]
We have 6 points, so the spline will have 5 segments.
Now let's define the ith spline functions as :
Si(t) = [xi(t), yi(t), zi(t)]
xi(t) = axi + bxi * t + cxi * t * t + dxi * t * t * t
yi(t) = ayi + byi * t + cyi * t * t + dyi * t * t * t
zi(t) = azi + bzi * t + czi * t * t + dzi * t * t * t
Where t parameter vary between [0, 1].
Special note : So let's define some Tj values for each points :
T0 = 0.0
T1 = 1.0
T2 = 2.0
T3 = 3.0
T4 = 4.0
T5 = 5.0
Since the t parameter vary in the range 0.0 to 1.0, we use the integer part of the Tj values to find out the spline segment we are on and the fractional part for the t parameter. You just need to be careful that when you reach the last Tj value, 'T5' in the given example, you need to force the segment to be 4 and also force the t parameter equal to 1.0.
Now let's define all the conditions :
n = 6
i = 0,1, ..., n - 1
Si(0) = Pi = [axi, ayi, azi]
Si(1) = Pi+1 = [axi + bxi + cxi + dxi, ayi + byi + cyi + dyi, azi + bzi + czi + dzi]
For interior points, as well as that the endpoints satisfy :
S0(0) = P0
Sn-1(1) = Pn
Taking the derivative of Si(t) in each interval then gives
S'i(0) = [Dxi, Dyi, Dzi] = [bxi, byi, bzi]
S'i(1) = [Dxi+1, Dyi+1, Dzi+1] = [bxi + 2 * cxi + 3 * dxi, byi + 2 * cyi + 3 * dyi, bzi + 2 * czi + 3 * dzi]
Now require that the first and second derivatives also match at the points, so
S'i-1(1) = S'i(0)
S''i-1(1) = S''i(0)
And finally the second derivative at the beginning and at the end of the spline need to be equal to 0.0.
S''0(0.0) = 0.0
S''n-1(1.0) = 0.0
The coefficients for the polynomials can be found :
[axi, ayi, azi] = Pi
[bxi, byi, bzi] = [Dxi, Dyi, Dzi]
[cxi, cyi, czi] = 3 * (Pi+1-Pi) - 2 * [Dxi, Dyi, Dzi] - [Dxi+1, Dyi+1, Dzi+1]
[dxi, dyi, dzi] = 2 * (Pi-Pi+1) + [Dxi, Dyi, Dzi] + [Dxi+1, Dyi+1, Dzi+1]
To solve this problem presenting it's self as A*x = r, a 6X6 tri-diagonal matrix 'A' need to be initialize like so :
[2 1 0 0 0 0]
[1 4 1 0 0 0]
[0 1 4 1 0 0]
[0 0 1 4 1 0]
[0 0 0 1 4 1]
[0 0 0 0 1 2]
A matrix 'x' need to be define like so (they are unknown we need to find)
[Dx0, Dy0, Dz0]
[Dx1, Dy1, Dz1]
[Dx2, Dy3, Dz2]
[Dx3, Dy3, Dz3]
[Dx4, Dy4, Dz4]
[Dx5, Dy5, Dz5]
The 'r' matrix need to be initialize like so :
[3 * (P1-P0)]
[3 * (P2-P0)]
[3 * (P3-P1)]
[3 * (P4-P2)]
[3 * (P5-P3)]
[3 * (P5-P4)]
With the given tri-diagonal matrix 'A', the Thomas algorithm can be used to solve for the matrix 'x'.
And then I have ask for a solution programmed in C (The solution can be translated in PureBasic afterward) and this is the solution the AI came up with :
Code: Select all
#include <stdio.h>
#include <stdlib.h>
typedef struct {
double x, y, z;
} Point3D;
typedef struct {
double a[3], b[3], c[3], d[3];
} SplineSegment;
void thomas_algorithm(int n, double a[], double b[], double c[], double d[], double x[]) {
double* c_prime = (double*)malloc(n * sizeof(double));
double* d_prime = (double*)malloc(n * sizeof(double));
c_prime[0] = c[0] / b[0];
d_prime[0] = d[0] / b[0];
for (int i = 1; i < n; i++) {
double m = 1.0 / (b[i] - a[i] * c_prime[i - 1]);
c_prime[i] = c[i] * m;
d_prime[i] = (d[i] - a[i] * d_prime[i - 1]) * m;
}
x[n - 1] = d_prime[n - 1];
for (int i = n - 2; i >= 0; i--) {
x[i] = d_prime[i] - c_prime[i] * x[i + 1];
}
free(c_prime);
free(d_prime);
}
void calculate_coefficients(int n, Point3D points[], Point3D derivatives[], SplineSegment splines[]) {
for (int i = 0; i < n - 1; i++) {
splines[i].a[0] = points[i].x;
splines[i].a[1] = points[i].y;
splines[i].a[2] = points[i].z;
splines[i].b[0] = derivatives[i].x;
splines[i].b[1] = derivatives[i].y;
splines[i].b[2] = derivatives[i].z;
splines[i].c[0] = 3 * (points[i + 1].x - points[i].x) - 2 * derivatives[i].x - derivatives[i + 1].x;
splines[i].c[1] = 3 * (points[i + 1].y - points[i].y) - 2 * derivatives[i].y - derivatives[i + 1].y;
splines[i].c[2] = 3 * (points[i + 1].z - points[i].z) - 2 * derivatives[i].z - derivatives[i + 1].z;
splines[i].d[0] = 2 * (points[i].x - points[i + 1].x) + derivatives[i].x + derivatives[i + 1].x;
splines[i].d[1] = 2 * (points[i].y - points[i + 1].y) + derivatives[i].y + derivatives[i + 1].y;
splines[i].d[2] = 2 * (points[i].z - points[i + 1].z) + derivatives[i].z + derivatives[i + 1].z;
}
}
Point3D evaluate_spline(SplineSegment spline, double t) {
Point3D result;
result.x = spline.a[0] + spline.b[0] * t + spline.c[0] * t * t + spline.d[0] * t * t * t;
result.y = spline.a[1] + spline.b[1] * t + spline.c[1] * t * t + spline.d[1] * t * t * t;
result.z = spline.a[2] + spline.b[2] * t + spline.c[2] * t * t + spline.d[2] * t * t * t;
return result;
}
int main(int argc, char **argv)
{
Point3D points[] = {
{0, 0, 0},
{15, 9, 4},
{19, 25, -20},
{34, 15, -13},
{26, -5, 2},
{20, -10, -9}
};
int n = sizeof(points) / sizeof(points[0]);
double a[n], b[n], c[n], dx[n], dy[n], dz[n], x[n], y[n], z[n];
b[0] = 2; b[n-1] = 2;
c[0] = 1; a[n-1] = 1;
for (int i = 1; i < n - 1; i++) {
a[i] = 1;
b[i] = 4;
c[i] = 1;
}
for (int i = 0; i < n; i++) {
if (i == 0 || i == n - 1) {
dx[i] = 3 * (points[i+1].x - points[i].x);
dy[i] = 3 * (points[i+1].y - points[i].y);
dz[i] = 3 * (points[i+1].z - points[i].z);
} else {
dx[i] = 3 * (points[i+1].x - points[i-1].x);
dy[i] = 3 * (points[i+1].y - points[i-1].y);
dz[i] = 3 * (points[i+1].z - points[i-1].z);
}
}
thomas_algorithm(n, a, b, c, dx, x);
thomas_algorithm(n, a, b, c, dy, y);
thomas_algorithm(n, a, b, c, dz, z);
Point3D derivatives[n];
for (int i = 0; i < n; i++) {
derivatives[i].x = x[i];
derivatives[i].y = y[i];
derivatives[i].z = z[i];
}
SplineSegment splines[n-1];
calculate_coefficients(n, points, derivatives, splines);
double T[] = {0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5 ,5};
for (int i = 0; i < 11; i++) {
int segment = (int)T[i];
double t = T[i] - segment;
if (segment >= n-1) {
segment = n-2;
t = 1.0;
}
Point3D result = evaluate_spline(splines[segment], t);
printf("T[%d] = %lf: (%lf, %lf, %lf)\n", i, T[i], result.x, result.y, result.z);
}
return 0;
}
Best regards
StarBootics
The Stone Age did not end due to a shortage of stones !