LongToRGB
Converts Long values (used by getpixel and other low level color functions) into RGB values that you can use. Useful if you want to manually remap an image or make some kind of color effect.
AI
AI Summary: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.
Source Code
Private Type zRGB
R As Long
G As Long
B As Long
End Type
Private Sub Form_Load()
'this is just an example
'if you don't tweak the code, you will have to
'dim a variable as "zRGB" that stores the returns
Dim cRGB As zRGB
cRGB = LongToRGB(RGB(255, 250, 255))
MsgBox cRGB.R & ", " & cRGB.G & ", " & cRGB.B
End
End Sub
Private Function LongToRGB(ColorValue As Long) As zRGB
Dim rCol As Long, gCol As Long, bCol As Long
rCol = ColorValue And &H10000FF 'this uses binary comparason
gCol = (ColorValue And &H100FF00) / (2 ^ 8)
bCol = (ColorValue And &H1FF0000) / (2 ^ 16)
LongToRGB.R = rCol
LongToRGB.G = gCol
LongToRGB.B = bCol
End Function
/*
root.cpp
Author: Ebon Bokody
Description: Numerical Analysis Assignment 1 -
Finds the positive root of the following equations to precision of at least
0.000001, using the bisection method, modified false position method, secant
method and newton's method. + Compare & comment on results & execution times
*/
#include <iostream.h>
#include <conio.h>
#include <math.h>
#include <time.h>
#include <sys/timeb.h>
// fn definitions
void bisection(int fn) ;
void mfp(int fn) ;
void secant(int fn) ;
void newton(int fn) ;
double f1(double) ; // evaluates function1
double f1_derivative(double) ;
double f2(double) ;
double f2_derivative(double) ;
void DispProb(int fn) ;
void main()
{
clrscr();
cout << "\n J2788 NUMERICAL ANALYSIS - ASSIGNMENT 1 - FINDING ROOTS"
<< "\n _______________________________________________________\n\n"
<< "Welcome to the finding roots program:\n\n\tProgrammer: Ebon Bokody"
<< "\n\tStudent ID: 13225966\n\n\nPress any key to begin!" ;
getch(); clrscr();
bisection(0) ; getch();
bisection(1) ;
cout << "\n\n\nPress any key to continue to modified false position method..." ;
getch(); clrscr();
mfp(0) ; getch();
mfp(1) ;
cout << "\n\n\nPress any key to continue to secant method..." ;
getch(); clrscr();
secant(0) ; getch();
secant(1) ;
cout << "\n\n\nPress any key to continue to newtons method..." ;
getch(); clrscr();
newton(0) ; getch();
newton(1) ;
cout << "\n\n\n\tGoodbye!" ;
return;
}
// ------------------------------------------------------------------------- //
void bisection(int fn)
{
double x, x0 = 0.5, x1 = 1.0, fx0 = f1(x0), fx = f1(x1) ;
int i = 0 ;
if (fn == 0)
cout << "\n USING BISECTION METHOD TO COMPUTE ROOTS"
<< "\n _______________________________________\n\n" ;
DispProb(fn);
while ((fx > 0.0000001 || fx < -0.0000001) && i < 20)
{
x = (x0 + x1)/2 ;
if (fn == 0) // first equation
fx = f1(x) ;
else // second equation
fx = f2(x) ;
if (fx0 * fx <= 0)
x1 = x ;
else
{ x0 = x ;
fx0 = fx ;
}
i++ ;
}
cout << "\n\nRoot: " << x0 << "\nIterations: " << i << "(max-20)" ;
return;
} // bisection
// ------------------------------------------------------------------------- //
void mfp(int fn)
{
if (fn == 0)
cout << "\n USING MODIFIED FALSE POSITION METHOD TO COMPUTE ROOTS"
<< "\n _____________________________________________________\n\n" ;
DispProb(fn) ;
double x, fx = 1, x0 = 0.5, x1 = 1.0, fx0 = f1(x0), fx1 = f1(x1), lfx = fx0 ;
int i =0 ;
while ((fx < -0.0000001 || fx > 0.0000001) && i < 20)
{
x = x0 - (fx0 * (x1 - x0)/(fx1 - fx0)) ;
if (fn == 0) // first equation
fx = f1(x) ;
else // second equation
fx = f2(x) ;
if (fx0 * fx <= 0)
{
x1 = x ;
fx1 = fx ;
if (lfx * fx > 0)
fx0 /= 2 ;
}
else
{
x0 = x ;
fx0 = fx ;
if (lfx * fx > 0)
fx1 /= 2 ;
}
lfx = fx ;
i++;
}
cout << "\n\nRoot: " << x0 << "\nIterations: " << i << "(max-20)" ;
} // mfp
// ------------------------------------------------------------------------- //
void secant(int fn)
{
if (fn == 0) // first time
cout << "\n USING SECANT METHOD TO COMPUTE ROOTS"
<< "\n ____________________________________\n\n" ;
DispProb(fn) ;
double fx0, fx1, x0 = 0.5, x1 = 1.0, x ;
int i =0 ;
fx0 = f1(x0) ;
fx1 = f1(x1) ;
while ((fx0 < -0.0000001 || fx0 > 0.0000001) && i < 20)
{
x = x1 - fx1*(x1 - x0)/(fx1 - fx0) ;
x0 = x1 ;
fx0 = fx1 ;
x1 = x ;
if (fn == 0) // first equation
fx1 = f1(x1) ;
else // second equation
fx1 = f2(x1) ;
i++;
}
cout << "\n\nRoot: " << x0 << "\nIterations: " << i << "(max-20)" ;
} // secant
// ------------------------------------------------------------------------- //
void newton(int fn)
{
double fx = 1, x1 = 1, x0 = 1 ;
int i = 0 ;
if (fn == 0)
cout << "\n USING NEWTON'S METHOD TO COMPUTE ROOTS"
<< "\n ______________________________________\n\n" ;
DispProb(fn) ;
while ((fx > 0.0000001 || fx < -0.0000001) && i < 20)
{
x0 = x1 ;
if (fn == 0) // first equation
{
fx = f1(x0) ;
x1 = x0 - fx/f1_derivative(x0) ;
}
else // second equation
{
fx = f2(x0) ;
x1 = x0 - fx/f2_derivative(x0) ;
}
i++ ;
}
cout << "\n\nRoot: " << x0 << "\nIterations: " << i << "(max-20)" ;
return;
} // newton
// ------------------------------------------------------------------------- //
double f1(double x)
{ return (x*x -exp(-x)) ; }
double f1_derivative(double x)
{ return (2*x + exp(-x)) ; }
// ------------------------------------------------------------------------- //
double f2(double x)
{ return (x*x*x - cos(x)) ; }
double f2_derivative(double x)
{ return (3*x*x + sin(x)) ; }
// ------------------------------------------------------------------------- //
void DispProb(int fn)
{
if (fn == 0)
cout << "We will now solve the equation : \n1)\t (x*x) - Exp(-x)\n\n"
<< "with original approximations of x0 = 0.5 and x1 = 1" ;
else
cout << "\n_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ \n"
<< "\nWe will now solve the equation : \n2)\t(x*x*x) - Cos(x)\n\n"
<< "with original approximations of x0 = 0.5 and x1 = 1.0" ;
}
Original Comments (3)
Recovered from Wayback Machine