Archive

Posts Tagged ‘C++’

3D vector distance calculation

July 24th, 2009 Athernar No comments

Recently I decided to revisit Vectors. Being something that while I covered in A-Level Physics,  but never really grasped beyond the basics.

So in the process of messing around with vectors, I wrote a small 3D vector distance calculator in PHP, which I then proceeded to port to C++ for fun. Code is below, and I’ll release the EXE if someone wants it, not really useful unless you want a quick(ish) way of getting distances I suppose.

Enjoy:

< ?php
 function distance($a, $b)
 {
  return sqrt(pow($a[0] - $b[0], 2) + pow($a[1] - $b[1], 2) + pow($a[2] - $b[2], 2));
 }

 $a = array(-1, 0, 6);
 $b = array(5, 2, 10);
 
 echo distance($a, $b); 
?>
#include <iostream>
#include <math .h>

using namespace std;

float distance(float &Bx, float &By, float &Bz)
{
 float Ax = 0.0f, Ay = 0.0f, Az = 0.0f;
 return sqrt(pow(Ax - Bx, 2) + pow(Ay - By, 2) + pow(Az - Bz, 2)); 
}

int main()
{
 float x, y, z;
 bool loop;

 do
 {
  cout < < "Enter vector:" << endl << "X: ";
  cin >> x;
  cout < < "Y: ";
  cin >> y;
  cout < < "Z: ";
  cin >> z;

  cout < < "Result: " << distance(x, y, z) << endl << "Again? 1/0: ";
  cin >> loop;
 } while(loop == true); 

 return 0;
}

Download: EXE ~10 KiB

Categories: Coding Tags: ,

A Lack of Patience

July 14th, 2009 Athernar 2 comments

I have a distinct lack of patience with C++ & Win32 App development. It’s the common method of Windows App development from what I’ve seen, but damn.

It’s probably my VB6/VB.NET roots showing here, but doing app layout and etc in the code feels ass-backwards and slow. I’m new to Win32 coding so I may be missing something, but I’m pretty convinced at this point I’m just too impatient to invest time into something I feel should be handled WYSIWYG. Hardcore C/C++ coders are probably chortling to themselves right now, going “Hurr hurr newbie”.

Anyhow, I’ve downloaded and installed Visual C# 2008 EE from Microsoft and I’ll see how app development goes in that. I have a firm grasp of C syntax, and I have a book on C# if the need arises, so I’ve got high hopes for this.

Next post I’ll hopefully have something to show.

Categories: Coding Tags: , ,
Easy AdSense by Unreal