Tag: C/C++
The programming language I utilized.
Blog Entry © Thursday, December 4, 2025, by James Pate Williams, Jr., Seven Methods to Solve the N-Queens Puzzle and Electron Scattering by a Hydrogen Atom
Blog Entry © Sunday, November 30, 2025, by James Pate Williams, Jr. and the Microsoft Artificially Intelligent Agent, the Copilot
Blog Entry © Saturday, November 28, 2025, by James Pate Williams, Jr. Fair Coin and Fair Dice Probabilities
Blog Entry © Tuesday 25, 2025, by James Pate Williams, Jr. Enumeration of the Tic-Tac-Toe Game Tree
/*
* Board.h
* Permutation
*
* Created by James Pate Williams, Jr. on 7/21/08.
* Modified on Monday 11/24/2025
* Copyright 2008 James Pate Williams, Jr. All rights reserved.
*
*/
#ifndef _Board_
#define _Board_
#include <fstream>
#include <vector>
class Board {
private:
char state[9];
std::vector<std::vector<char>> board;
public:
Board(void);
Board(const Board &board);
Board(const char b[3][3]);
Board(const std::vector<char> &v);
char GetState(int n) const;
void PrintBoard(int n) const;
void WriteBoard(std::ofstream &out) const;
friend bool operator == (const Board &b1, const Board &b2);
friend bool operator != (const Board &b1, const Board &b2);
friend bool Found(const Board &initial, const Board &board);
friend bool Win(const Board b, char winner[8]);
friend bool LegalBoard(
bool& legal, bool& won, char& whoWon, const Board b);
friend void PrintBoard(char board[3][3], int number);
static const char CharX, CharO, CharS;
};
#endif
/*
* Board.cpp
* Permutation
*
* Created by James Pate Williams, Jr. on 7/21/08.
* Copyright 2008 James Pate Williams, Jr. All rights reserved.
*
*/
#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <vector>
#include "Board.h"
const char Board::CharO = 'o';
const char Board::CharX = 'x';
const char Board::CharS = '_';
Board::Board(void) {
board.resize(3, std::vector<char>(3));
for (int i = 0; i < 9; i++)
state[i] = '\0';
}
Board::Board(const Board &b) {
board.resize(3, std::vector<char>(3));
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
state[3 * i + j] = board[i][j] = b.state[3 * i + j];
}
Board::Board(const char b[3][3]) {
board.resize(3, std::vector<char>(3));
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
state[3 * i + j] = board[i][j] = b[i][j];
}
Board::Board(const std::vector<char> &v) {
board.resize(3, std::vector<char>(3));
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
state[3 * i + j] = board[i][j] = v[3LL * i + j];
}
char Board::GetState(int n) const {
return state[n];
}
void Board::PrintBoard(int n) const {
std::cout << n << std::endl;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++)
std::cout << state[3 * i + j];
std::cout << std::endl;
}
std::cout << std::endl;
std::cout << std::endl;
}
void Board::WriteBoard(std::ofstream &out) const {
for (int i = 0; i < 9; i++)
out << state[i];
out << std::endl;
}
bool operator == (const Board &b1, const Board &b2) {
int i, equal = 0;
for (i = 0; i < 9; i++)
if (b1.state[i] == b2.state[i])
equal++;
return equal == 9;
}
bool operator != (const Board &b1, const Board &b2) {
for (int i = 0; i < 9; i++)
if (b1.state[i] == b2.state[i])
return false;
return true;
}
bool Found(const Board &initial, const Board &board) {
// reflections
if (initial.state[0] == board.state[6] && initial.state[1] == board.state[7] &&
initial.state[2] == board.state[8] && initial.state[3] == board.state[3] &&
initial.state[4] == board.state[4] && initial.state[5] == board.state[5] &&
initial.state[6] == board.state[0] && initial.state[7] == board.state[1] &&
initial.state[8] == board.state[2])
return true;
if (initial.state[0] == board.state[2] && initial.state[1] == board.state[1] &&
initial.state[2] == board.state[0] && initial.state[3] == board.state[5] &&
initial.state[4] == board.state[4] && initial.state[5] == board.state[3] &&
initial.state[6] == board.state[8] && initial.state[7] == board.state[7] &&
initial.state[8] == board.state[6])
return true;
if (initial.state[0] == board.state[0] && initial.state[1] == board.state[3] &&
initial.state[2] == board.state[6] && initial.state[3] == board.state[1] &&
initial.state[4] == board.state[4] && initial.state[5] == board.state[7] &&
initial.state[6] == board.state[2] && initial.state[7] == board.state[5] &&
initial.state[8] == board.state[8])
return true;
if (initial.state[0] == board.state[8] && initial.state[1] == board.state[5] &&
initial.state[2] == board.state[2] && initial.state[3] == board.state[7] &&
initial.state[4] == board.state[4] && initial.state[5] == board.state[1] &&
initial.state[6] == board.state[6] && initial.state[7] == board.state[3] &&
initial.state[8] == board.state[0])
return true;
// rotations
if (initial.state[0] == board.state[6] && initial.state[1] == board.state[3] &&
initial.state[2] == board.state[0] && initial.state[3] == board.state[7] &&
initial.state[4] == board.state[4] && initial.state[5] == board.state[1] &&
initial.state[6] == board.state[8] && initial.state[7] == board.state[5] &&
initial.state[8] == board.state[2])
return true;
if (initial.state[0] == board.state[8] && initial.state[1] == board.state[7] &&
initial.state[2] == board.state[6] && initial.state[3] == board.state[5] &&
initial.state[4] == board.state[4] && initial.state[5] == board.state[3] &&
initial.state[6] == board.state[2] && initial.state[7] == board.state[1] &&
initial.state[8] == board.state[0])
return true;
if (initial.state[0] == board.state[2] && initial.state[1] == board.state[5] &&
initial.state[2] == board.state[8] && initial.state[3] == board.state[1] &&
initial.state[4] == board.state[4] && initial.state[5] == board.state[7] &&
initial.state[6] == board.state[0] && initial.state[7] == board.state[3] &&
initial.state[8] == board.state[6])
return true;
// equal
return initial == board;
}
bool Win(const Board b, char winner[8]) {
bool win = false;
for (int i = 0; i < 8; i++)
winner[i] = Board::CharS;
if (b.board[0][0] == b.board[0][1] && b.board[0][0] ==
b.board[0][2] && b.board[0][0] != Board::CharS) {
winner[0] = b.board[0][0];
win = true;
}
if (b.board[1][0] == b.board[1][1] && b.board[1][0] ==
b.board[1][2] && b.board[1][0] != Board::CharS) {
winner[1] = b.board[1][0];
win = true;
}
if (b.board[2][0] == b.board[2][1] && b.board[2][0] ==
b.board[2][2] && b.board[2][0] != Board::CharS) {
winner[2] = b.board[2][0];
win = true;
}
if (b.board[0][0] == b.board[1][0] && b.board[0][0] ==
b.board[2][0] && b.board[0][0] != Board::CharS) {
winner[3] = b.board[0][0];
win = true;
}
if (b.board[0][1] == b.board[1][1] && b.board[0][1] ==
b.board[2][1] && b.board[0][1] != Board::CharS) {
winner[4] = b.board[0][1];
win = true;
}
if (b.board[0][2] == b.board[1][2] && b.board[0][2] ==
b.board[2][2] && b.board[0][2] != Board::CharS) {
winner[5] = b.board[0][2];
win = true;
}
if (b.board[0][0] == b.board[1][1] && b.board[0][0] ==
b.board[2][2] && b.board[0][0] != Board::CharS) {
winner[6] = b.board[0][0];
win = true;
}
if (b.board[0][2] == b.board[1][1] && b.board[0][2] ==
b.board[2][0] && b.board[0][2] != Board::CharS) {
winner[7] = b.board[0][2];
win = true;
}
return win;
}
void PrintBoard(char board[3][3], int number) {
std::cout << "number = " << number << std::endl;
std::cout << std::endl;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++)
std::cout << board[i][j];
std:: cout << std::endl;
}
std::cout << std::endl;
}
bool LegalBoard(
bool &legal, bool &won, char &whoWon, const Board b) {
bool winO, winX;
char winner[8];
int countO = 0, countS = 0, countX = 0;
int i, j, oWins = 0, xWins = 0;
legal = won = false;
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
if (b.board[i][j] == Board::CharO)
countO++;
else if (b.board[i][j] == Board::CharX)
countX++;
else if (b.board[i][j] == Board::CharS)
countS++;
}
}
if ((countX >= 3 || countO >= 3) && Win(b, winner)) {
for (i = 0; i < 8; i++) {
if (winner[i] == Board::CharX)
xWins++;
else if (winner[i] == Board::CharO)
oWins++;
}
winX = (xWins >= 1 && oWins == 0) &&
(countX <= 5 && countO <= 4 && countX == countO + 1);
winO = (xWins == 0 && oWins >= 1) &&
(countO <= 5 && countX <= 4 && countO == countX + 1);
legal = won = winX || winO;
if (won && winX)
whoWon = Board::CharX;
else if (won && winO)
whoWon = Board::CharO;
}
else
legal = (countX <= 5 && countO <= 4 && countX == countO + 1) ||
(countO <= 5 && countX <= 4 && countO == countX + 1);
return legal;
}
#define _WriteBV_
#include <algorithm>
#ifdef _WriteBV_
#include <fstream>
#endif
#include <iomanip>
#include <iostream>
#include <vector>
#include "Board.h"
static int Compare(const void *vPtr1, const void *vPtr2) {
char *cPtr1 = (char *) vPtr1;
char *cPtr2 = (char *) vPtr2;
return strcmp(cPtr1, cPtr2);
}
static void EnumerateTicTacToe(void) {
bool legal, won;
char b[3][3], whoWon;
char tree[3][3][3][3][3][3][3][3][3];
int i0, i1, i2, i3, i4, i5, i6, i7, i8;
int legals = 0, number = 0, wCount = 0;
int xCount = 0, oCount = 0;
int bvSize, i, j;
std::vector<Board> boardVector;
for (i0 = 0; i0 < 3; i0++) {
for (i1 = 0; i1 < 3; i1++) {
for (i2 = 0; i2 < 3; i2++) {
for (i3 = 0; i3 < 3; i3++) {
for (i4 = 0; i4 < 3; i4++) {
for (i5 = 0; i5 < 3; i5++) {
for (i6 = 0; i6 < 3; i6++) {
for (i7 = 0; i7 < 3; i7++) {
for (i8 = 0; i8 < 3; i8++) {
if (i0 == 0)
b[0][0] = tree[i0][i1][i2][i3][i4][i5][i6][i7][i8] = Board::CharX;
else if (i0 == 1)
b[0][0] = tree[i0][i1][i2][i3][i4][i5][i6][i7][i8] = Board::CharO;
else if (i0 == 2)
b[0][0] = tree[i0][i1][i2][i3][i4][i5][i6][i7][i8] = Board::CharS;
if (i1 == 0)
b[0][1] = tree[i0][i1][i2][i3][i4][i5][i6][i7][i8] = Board::CharX;
else if (i1 == 1)
b[0][1] = tree[i0][i1][i2][i3][i4][i5][i6][i7][i8] = Board::CharO;
else if (i1 == 2)
b[0][1] = tree[i0][i1][i2][i3][i4][i5][i6][i7][i8] = Board::CharS;
if (i2 == 0)
b[0][2] = tree[i0][i1][i2][i3][i4][i5][i6][i7][i8] = Board::CharX;
else if (i2 == 1)
b[0][2] = tree[i0][i1][i2][i3][i4][i5][i6][i7][i8] = Board::CharO;
else if (i2 == 2)
b[0][2] = tree[i0][i1][i2][i3][i4][i5][i6][i7][i8] = Board::CharS;
if (i3 == 0)
b[1][0] = tree[i0][i1][i2][i3][i4][i5][i6][i7][i8] = Board::CharX;
else if (i3 == 1)
b[1][0] = tree[i0][i1][i2][i3][i4][i5][i6][i7][i8] = Board::CharO;
else if (i3 == 2)
b[1][0] = tree[i0][i1][i2][i3][i4][i5][i6][i7][i8] = Board::CharS;
if (i4 == 0)
b[1][1] = tree[i0][i1][i2][i3][i4][i5][i6][i7][i8] = Board::CharX;
else if (i4 == 1)
b[1][1] = tree[i0][i1][i2][i3][i4][i5][i6][i7][i8] = Board::CharO;
else if (i4 == 2)
b[1][1] = tree[i0][i1][i2][i3][i4][i5][i6][i7][i8] = Board::CharS;
if (i5 == 0)
b[1][2] = tree[i0][i1][i2][i3][i4][i5][i6][i7][i8] = Board::CharX;
else if (i5 == 1)
b[1][2] = tree[i0][i1][i2][i3][i4][i5][i6][i7][i8] = Board::CharO;
else if (i5 == 2)
b[1][2] = tree[i0][i1][i2][i3][i4][i5][i6][i7][i8] = Board::CharS;
if (i6 == 0)
b[2][0] = tree[i0][i1][i2][i3][i4][i5][i6][i7][i8] = Board::CharX;
else if (i6 == 1)
b[2][0] = tree[i0][i1][i2][i3][i4][i5][i6][i7][i8] = Board::CharO;
else if (i6 == 2)
b[2][0] = tree[i0][i1][i2][i3][i4][i5][i6][i7][i8] = Board::CharS;
if (i7 == 0)
b[2][1] = tree[i0][i1][i2][i3][i4][i5][i6][i7][i8] = Board::CharX;
else if (i7 == 1)
b[2][1] = tree[i0][i1][i2][i3][i4][i5][i6][i7][i8] = Board::CharO;
else if (i7 == 2)
b[2][1] = tree[i0][i1][i2][i3][i4][i5][i6][i7][i8] = Board::CharS;
if (i8 == 0)
b[2][2] = tree[i0][i1][i2][i3][i4][i5][i6][i7][i8] = Board::CharX;
else if (i8 == 1)
b[2][2] = tree[i0][i1][i2][i3][i4][i5][i6][i7][i8] = Board::CharO;
else if (i8 == 2)
b[2][2] = tree[i0][i1][i2][i3][i4][i5][i6][i7][i8] = Board::CharS;
Board bb(b);
if (LegalBoard(legal, won, whoWon, bb)) {
Board board(b);
legals++;
if (won) {
wCount++;
if (whoWon == Board::CharX)
xCount++;
else if (whoWon == Board::CharO)
oCount++;
}
boardVector.push_back(board);
}
number++;
}}}}}}}}}
for (i = 0; i < boardVector.size() - 1; i++)
for (j = i; j < boardVector.size(); j++)
if (Found(boardVector[i], boardVector[j]))
boardVector.erase(boardVector.begin() + j);
bvSize = (int)boardVector.size();
std::cout << std::setw(4) << "legal total = " << legals << std::endl;
std::cout << std::setw(4) << "xWins total = " << xCount << std::endl;
std::cout << std::setw(4) << "oWins total = " << xCount << std::endl;
std::cout << std::setw(4) << "oxWin total = " << xCount + oCount << std::endl;
std::cout << std::setw(4) << "unique size = " << boardVector.size();
std::cout << std::endl;
#ifdef _WriteBV_
std::cout << std::endl;
std::ofstream outFile("Tic TacToe Game Tree Enumeration.txt");
std::cout << std::endl;
for (i = 0; i < bvSize; i++) {
boardVector[i].PrintBoard(i + 1);
boardVector[i].WriteBoard(outFile);
}
#endif
}
int main (int argc, char * const argv[]) {
EnumerateTicTacToe();
return 0;
}
Blog Entry © Monday, November 24, 2025, by James Pate Williams, Jr., A* Informed Search Application to Solve the 15-Tile Puzzle
Blog Entry © Sunday, November 23, 2025, by James Pate Williams, Jr. Modification of My A* Informed Search Solver for the 8-Tile Puzzle
Blog Entry © Friday, November 21, 2025, by James Pate Williams, Jr., Win32 C/C++ Desktop GUI Solution of the 15-Tile Puzzle Using Iterative Deepening A* Informed Seach Algorithm
Blog Entry © Monday, November 17, 2025, by James Pate Williams, Jr. An Elitist Evolutionary Hill Climber to Solve the 8-Tile Puzzle
Some General Relativity Mathematics by James Pate Williams, Jr. (c) July 13, 2025
// SomeRelativityMath.cpp : Defines the entry point for the application.
// https://arxiv.org/pdf/2506.09946
#include "pch.h"
#include "framework.h"
#include "SomeRelativityMath.h"
#include <stdio.h>
#include <vector>
#define MAX_LOADSTRING 100
typedef struct tagPoint2d
{
double x, y;
} POINT2D, * PPOINT2D;
// Global Variables:
HINSTANCE hInst; // current instance
WCHAR szTitle[MAX_LOADSTRING]; // The title bar text
WCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
char text[8192];
std::vector<POINT2D> points1, points2;
std::vector<POINT2D> points3, points4;
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK DrawGraph(HWND, UINT, WPARAM, LPARAM);
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: Place code here.
// Initialize global strings
LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadStringW(hInstance, IDC_SOMERELATIVITYMATH, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_SOMERELATIVITYMATH));
MSG msg;
// Main message loop:
while (GetMessage(&msg, nullptr, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEXW wcex = { 0 };
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_SOMERELATIVITYMATH));
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_SOMERELATIVITYMATH);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassExW(&wcex);
}
//
// FUNCTION: InitInstance(HINSTANCE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance; // Store instance handle in our global variable
HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
static void FindMinMax(
double& xMin, double& xMax,
double& yMin, double& yMax)
{
// uses global 2D double points structure
xMin = yMin = DBL_MAX;
xMax = yMax = DBL_MIN;
for (size_t i = 0; i < points1.size(); i++)
{
POINT2D pt = points1[i];
double x = pt.x;
double y = pt.y;
if (x < xMin)
xMin = x;
if (x > xMax)
xMax = x;
if (y < yMin)
yMin = y;
if (y > yMax)
yMax = y;
}
for (size_t i = 0; i < points2.size(); i++)
{
POINT2D pt = points2[i];
double x = pt.x;
double y = pt.y;
if (x < xMin)
xMin = x;
if (x > xMax)
xMax = x;
if (y < yMin)
yMin = y;
if (y > yMax)
yMax = y;
}
for (size_t i = 0; i < points3.size(); i++)
{
POINT2D pt = points3[i];
double x = pt.x;
double y = pt.y;
if (x < xMin)
xMin = x;
if (x > xMax)
xMax = x;
if (y < yMin)
yMin = y;
if (y > yMax)
yMax = y;
}
for (size_t i = 0; i < points4.size(); i++)
{
POINT2D pt = points4[i];
double x = pt.x;
double y = pt.y;
if (x < xMin)
xMin = x;
if (x > xMax)
xMax = x;
if (y < yMin)
yMin = y;
if (y > yMax)
yMax = y;
}
}
static void DrawFormattedText(HDC hdc, char text[], RECT rect)
{
// Draw the text with formatting options
DrawTextA(hdc, text, -1, &rect, DT_SINGLELINE | DT_NOCLIP);
}
//
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
{
double P1 = 0, P2 = 0, P3 = 0, P4 = 0;
int numberPts = 1024;
double x0 = 1, h = 0.5 / numberPts, x = x0;
points1.resize(numberPts);
points2.resize(numberPts);
points3.resize(numberPts);
points4.resize(numberPts);
for (int i = 0; i < numberPts; i++)
{
P1 = x + 1.0;
P2 = 3.0 * x * x + 4.0 * x + 2.0;
P3 = 15.0 * x * x * x + 16.0 * x * x + 6.0 * x;
P4 = 105.0 * x * x * x * x + 156.0 * x * x * x +
94.0 * x * x + 24.0 * x;
POINT2D pt = { 0 };
pt.x = x;
pt.y = log(P1);
points1[i] = pt;
pt.y = log(P2);
points2[i] = pt;
pt.y = log(P3);
points3[i] = pt;
pt.y = log(P4);
points4[i] = pt;
x += h;
}
break;
}
case WM_COMMAND:
{
int wmId = LOWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
break;
case WM_PAINT:
{
double h = 0, pi = 0, plm = 0, theta = 0;
double xMax = 0, xMin = 0, yMax = 0, yMin = 0;
FindMinMax(xMin, xMax, yMin, yMax);
float xSpan = (float)(xMax - xMin);
float ySpan = (float)(yMax - yMin);
RECT rect = { };
GetClientRect(hWnd, &rect);
float width = (float)(rect.right - rect.left + 1);
float height = (float)(rect.bottom - rect.top - 32 + 1);
float sx0 = 2.0f * width / 16.0f;
float sx1 = 14.0f * width / 16.0f;
float sy0 = 2.0f * height / 16.0f;
float sy1 = 14.0f * height / 16.0f;
float deltaX = xSpan / 8.0f;
float deltaY = ySpan / 8.0f;
float xSlope = (sx1 - sx0) / xSpan;
float xInter = (float)(sx0 - xSlope * xMin);
float ySlope = (sy0 - sy1) / ySpan;
float yInter = (float)(sy0 - ySlope * yMax);
float px = 0, py = 0, sx = 0, sy = 0;
PAINTSTRUCT ps;
POINT wPt = { };
HDC hdc = BeginPaint(hWnd, &ps);
int i = 0;
float x = (float)xMin;
float y = (float)yMax;
px = x;
py = y;
sx = xSlope * px + xInter;
sy = ySlope * py + yInter;
MoveToEx(hdc, (int)sx, (int)sy0, &wPt);
char buffer[128] = { };
while (i <= 8)
{
sx = xSlope * x + xInter;
wPt.x = wPt.y = 0;
MoveToEx(hdc, (int)sx, (int)sy0, &wPt);
LineTo(hdc, (int)sx, (int)sy1);
sprintf_s(buffer, "%3.2f", x);
SIZE size = { };
GetTextExtentPoint32A(
hdc,
buffer,
strlen(buffer),
&size);
RECT textRect = { };
textRect.left = (long)(sx - size.cx / 2.0f);
textRect.right = (long)(sx + size.cx / 2.0f);
textRect.top = (long)sy1;
textRect.bottom = (long)(sy1 + size.cy / 2.0f);
DrawFormattedText(hdc, buffer, textRect);
x += deltaX;
i++;
}
i = 0;
y = (float)yMin;
while (i <= 8)
{
sy = ySlope * y + yInter;
wPt.x = wPt.y = 0;
MoveToEx(hdc, (int)sx0, (int)sy, &wPt);
LineTo(hdc, (int)sx, (int)sy);
if (i != 0)
{
sprintf_s(buffer, "%3.1f", y);
SIZE size = { };
GetTextExtentPoint32A(
hdc,
buffer,
strlen(buffer),
&size);
RECT textRect = { };
textRect.left = (long)(sx0 - size.cx - size.cx / 5.0f);
textRect.right = (long)(sx0 - size.cx / 2.0f);
textRect.top = (long)(sy - size.cy / 2.0f);
textRect.bottom = (long)(sy + size.cy / 2.0f);
DrawFormattedText(hdc, buffer, textRect);
}
y += deltaY;
i++;
}
HGDIOBJ rPenNew = NULL;
HGDIOBJ hPenOld = NULL;
rPenNew = CreatePen(PS_SOLID, 2, RGB(255, 0, 0));
hPenOld = SelectObject(hdc, rPenNew);
px = (float)points1[0].x;
py = (float)points1[0].y;
sx = xSlope * px + xInter;
sy = ySlope * py + yInter;
wPt.x = wPt.y = 0;
MoveToEx(hdc, (int)sx, (int)sy, &wPt);
for (size_t j = 1; j < points1.size(); j++)
{
px = (float)points1[j].x;
py = (float)points1[j].y;
sx = xSlope * px + xInter;
sy = ySlope * py + yInter;
LineTo(hdc, (int)sx, (int)sy);
}
SelectObject(hdc, hPenOld);
DeleteObject(rPenNew);
HGDIOBJ gPenNew = CreatePen(PS_SOLID, 2, RGB(0, 255, 0));
hPenOld = SelectObject(hdc, gPenNew);
px = (float)points2[0].x;
py = (float)points2[0].y;
sx = xSlope * px + xInter;
sy = ySlope * py + yInter;
wPt.x = wPt.y = 0;
MoveToEx(hdc, (int)sx, (int)sy, &wPt);
for (size_t j = 1; j < points2.size(); j++)
{
px = (float)points2[j].x;
py = (float)points2[j].y;
sx = xSlope * px + xInter;
sy = ySlope * py + yInter;
LineTo(hdc, (int)sx, (int)sy);
}
SelectObject(hdc, hPenOld);
DeleteObject(gPenNew);
HGDIOBJ bPenNew = CreatePen(PS_SOLID, 2, RGB(0, 0, 255));
hPenOld = SelectObject(hdc, bPenNew);
px = (float)points3[0].x;
py = (float)points3[0].y;
sx = xSlope * px + xInter;
sy = ySlope * py + yInter;
wPt.x = wPt.y = 0;
MoveToEx(hdc, (int)sx, (int)sy, &wPt);
for (size_t j = 1; j < points3.size(); j++)
{
px = (float)points3[j].x;
py = (float)points3[j].y;
sx = xSlope * px + xInter;
sy = ySlope * py + yInter;
LineTo(hdc, (int)sx, (int)sy);
}
SelectObject(hdc, hPenOld);
DeleteObject(bPenNew);
HGDIOBJ pPenNew = CreatePen(PS_SOLID, 2, RGB(128, 0, 128));
hPenOld = SelectObject(hdc, pPenNew);
px = (float)points4[0].x;
py = (float)points4[0].y;
sx = xSlope * px + xInter;
sy = ySlope * py + yInter;
wPt.x = wPt.y = 0;
MoveToEx(hdc, (int)sx, (int)sy, &wPt);
for (size_t j = 1; j < points4.size(); j++)
{
px = (float)points4[j].x;
py = (float)points4[j].y;
sx = xSlope * px + xInter;
sy = ySlope * py + yInter;
LineTo(hdc, (int)sx, (int)sy);
}
SelectObject(hdc, hPenOld);
DeleteObject(pPenNew);
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
static INT_PTR CALLBACK DrawGraph(
HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
char line[256] = { };
switch (message)
{
case WM_INITDIALOG:
GetWindowTextA(hDlg, line, 256);
strcat_s(text, 8192, " ");
strcat_s(text, 8192, line);
SetWindowTextA(hDlg, text);
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
case WM_PAINT:
double h = 0, pi = 0, plm = 0, theta = 0;
double xMax = 0, xMin = 0, yMax = 0, yMin = 0;
FindMinMax(xMin, xMax, yMin, yMax);
float xSpan = (float)(xMax - xMin);
float ySpan = (float)(yMax - yMin);
RECT rect = { };
GetClientRect(hDlg, &rect);
float width = (float)(rect.right - rect.left + 1);
float height = (float)(rect.bottom - rect.top - 32 + 1);
float sx0 = 2.0f * width / 16.0f;
float sx1 = 14.0f * width / 16.0f;
float sy0 = 2.0f * height / 16.0f;
float sy1 = 14.0f * height / 16.0f;
float deltaX = xSpan / 8.0f;
float deltaY = ySpan / 8.0f;
float xSlope = (sx1 - sx0) / xSpan;
float xInter = (float)(sx0 - xSlope * xMin);
float ySlope = (sy0 - sy1) / ySpan;
float yInter = (float)(sy0 - ySlope * yMax);
float px = 0, py = 0, sx = 0, sy = 0;
PAINTSTRUCT ps;
POINT wPt = { };
HDC hdc = BeginPaint(hDlg, &ps);
int i = 0;
float x = (float)xMin;
float y = (float)yMax;
px = x;
py = y;
sx = xSlope * px + xInter;
sy = ySlope * py + yInter;
MoveToEx(hdc, (int)sx, (int)sy0, &wPt);
char buffer[128] = { };
while (i <= 8)
{
sx = xSlope * x + xInter;
wPt.x = wPt.y = 0;
MoveToEx(hdc, (int)sx, (int)sy0, &wPt);
LineTo(hdc, (int)sx, (int)sy1);
sprintf_s(buffer, "%3.0f", x);
SIZE size = { };
GetTextExtentPoint32A(
hdc,
buffer,
strlen(buffer),
&size);
RECT textRect = { };
textRect.left = (long)(sx - size.cx / 2.0f);
textRect.right = (long)(sx + size.cx / 2.0f);
textRect.top = (long)sy1;
textRect.bottom = (long)(sy1 + size.cy / 2.0f);
DrawFormattedText(hdc, buffer, textRect);
x += deltaX;
i++;
}
i = 0;
y = (float)yMin;
while (i <= 8)
{
sy = ySlope * y + yInter;
wPt.x = wPt.y = 0;
MoveToEx(hdc, (int)sx0, (int)sy, &wPt);
LineTo(hdc, (int)sx, (int)sy);
if (i != 0)
{
sprintf_s(buffer, "%3.0f", y);
SIZE size = { };
GetTextExtentPoint32A(
hdc,
buffer,
strlen(buffer),
&size);
RECT textRect = { };
textRect.left = (long)(sx0 - size.cx - size.cx / 5.0f);
textRect.right = (long)(sx0 - size.cx / 2.0f);
textRect.top = (long)(sy - size.cy / 2.0f);
textRect.bottom = (long)(sy + size.cy / 2.0f);
DrawFormattedText(hdc, buffer, textRect);
}
y += deltaY;
i++;
}
HGDIOBJ rPenNew = NULL;
HGDIOBJ hPenOld = NULL;
rPenNew = CreatePen(PS_SOLID, 2, RGB(255, 0, 0));
hPenOld = SelectObject(hdc, rPenNew);
px = (float)points1[1].x;
py = (float)points1[1].y;
sx = xSlope * px + xInter;
sy = ySlope * py + yInter;
wPt.x = wPt.y = 0;
MoveToEx(hdc, (int)sx, (int)sy, &wPt);
for (size_t j = 1; j < points1.size(); j++)
{
px = (float)points1[j].x;
py = (float)points1[j].y;
sx = xSlope * px + xInter;
sy = ySlope * py + yInter;
LineTo(hdc, (int)sx, (int)sy);
}
SelectObject(hdc, hPenOld);
DeleteObject(rPenNew);
px = (float)points2[1].x;
py = (float)points2[1].y;
sx = xSlope * px + xInter;
sy = ySlope * py + yInter;
wPt.x = wPt.y = 0;
MoveToEx(hdc, (int)sx, (int)sy, &wPt);
HGDIOBJ gPenNew = CreatePen(PS_SOLID, 2, RGB(0, 255, 0));
hPenOld = SelectObject(hdc, gPenNew);
px = (float)points2[1].x;
py = (float)points2[1].y;
sx = xSlope * px + xInter;
sy = ySlope * py + yInter;
wPt.x = wPt.y = 0;
MoveToEx(hdc, (int)sx, (int)sy, &wPt);
for (size_t j = 1; j < points2.size(); j++)
{
px = (float)points2[j].x;
py = (float)points2[j].y;
sx = xSlope * px + xInter;
sy = ySlope * py + yInter;
LineTo(hdc, (int)sx, (int)sy);
}
SelectObject(hdc, hPenOld);
DeleteObject(gPenNew);
px = (float)points3[1].x;
py = (float)points3[1].y;
sx = xSlope * px + xInter;
sy = ySlope * py + yInter;
wPt.x = wPt.y = 0;
MoveToEx(hdc, (int)sx, (int)sy, &wPt);
HGDIOBJ bPenNew = CreatePen(PS_SOLID, 2, RGB(0, 0, 255));
hPenOld = SelectObject(hdc, bPenNew);
px = (float)points3[1].x;
py = (float)points3[1].y;
sx = xSlope * px + xInter;
sy = ySlope * py + yInter;
wPt.x = wPt.y = 0;
MoveToEx(hdc, (int)sx, (int)sy, &wPt);
for (size_t j = 1; j < points3.size(); j++)
{
px = (float)points3[j].x;
py = (float)points3[j].y;
sx = xSlope * px + xInter;
sy = ySlope * py + yInter;
LineTo(hdc, (int)sx, (int)sy);
}
SelectObject(hdc, hPenOld);
DeleteObject(bPenNew);
px = (float)points4[1].x;
py = (float)points4[1].y;
sx = xSlope * px + xInter;
sy = ySlope * py + yInter;
wPt.x = wPt.y = 0;
MoveToEx(hdc, (int)sx, (int)sy, &wPt);
HGDIOBJ tPenNew = CreatePen(PS_SOLID, 2, RGB(128, 0, 128));
hPenOld = SelectObject(hdc, tPenNew);
px = (float)points4[1].x;
py = (float)points4[1].y;
sx = xSlope * px + xInter;
sy = ySlope * py + yInter;
wPt.x = wPt.y = 0;
MoveToEx(hdc, (int)sx, (int)sy, &wPt);
for (size_t j = 1; j < points4.size(); j++)
{
px = (float)points4[j].x;
py = (float)points4[j].y;
sx = xSlope * px + xInter;
sy = ySlope * py + yInter;
LineTo(hdc, (int)sx, (int)sy);
}
SelectObject(hdc, hPenOld);
DeleteObject(tPenNew);
EndPaint(hDlg, &ps);
return (INT_PTR)FALSE;
break;
}
return (INT_PTR)FALSE;
}