I have an object in 3D space that can give me its rotational status in radians, degrees or as a quaternion.
How do I turn these angles into a vector that I can work with?
I'm planning to make an object "go-infront" of my view camera, just getting the aimvector.
[QUOTE=Silentfood;38397908]I have an object in 3D space that can give me its rotational status in radians, degrees or as a quaternion.
How do I turn these angles into a vector that I can work with?
I'm planning to make an object "go-infront" of my view camera, just getting the aimvector.[/QUOTE]
IIRC if you get a normalized quaternion, the first 3 components are a unit vector in the direction the object is facing.
time for C++ homework help!
this program doesn't have a real purpose by itself, it's just a collection of 7 tests to see if the user can input various text inputs correctly.
first here's the whole program (dont have a heart attack, most of this was supplied by the teacher, i will repost the small part i created at the bottom)
[cpp]// Gaddis Ch 10 - C. Butterball - 121020
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
bool editDelimiter(char, int, string);
bool editSSN(string);
bool editID(string);
bool editPasswrd(string);
void getFields(char, int, string, string[]);
void displayFields(string[], int);
void trimSpace(string&,int);
void trimLead(string&);
void trimTrail(string&);
void trimAll(string&);
void test1()
{
cout << "\nTEST 1...\n\n"; //edit input line and extract fields
string testLine, fields[10];
char delimiter; int delimCnt; bool editOK;
cout << "What is the delimiter character? ";
cin >> delimiter;
cout << "What is valid delimiter count? ";
cin >> delimCnt;
cin.ignore(100,'\n');
cout << "Enter a line to be tested: ";
getline(cin, testLine);
editOK = editDelimiter(delimiter, delimCnt, testLine);
if(editOK)
{
cout << "...PASSED EDIT\n";
getFields(delimiter, delimCnt, testLine, fields);
displayFields(fields, delimCnt);
}
else
cout << "...***FAILED EDIT***\n";
return;
}
void test2()
{
cout << "\nTEST 2...\n\n"; //edit a name field
string nameField;
const string EMPTY = ""; const int TRIM_CODE = 3;
cin.ignore(100,'\n');
cout << "Enter a name field to be tested: ";
getline(cin, nameField);
if(nameField != EMPTY)
{
cout << "...PASSED NULL EDIT\n";
trimSpace(nameField,TRIM_CODE);
cout << "Edited Name Field: " << nameField;
cout << "\n...length: " << nameField.length(); //not required by specs
}
else
cout << "...***FAILED NULL EDIT***\n";
return;
}
void test3()
{
cout << "\nTEST 3...\n\n"; //edit a SSN field
string ssnField; bool editOK;
const string EMPTY = ""; const int TRIM_CODE = 3;
cin.ignore(100,'\n');
cout << "Enter a SSN field to be tested: ";
getline(cin, ssnField);
if(ssnField != EMPTY)
{
cout << "...PASSED NULL EDIT\n";
trimSpace(ssnField,TRIM_CODE);
cout << "Trimmed SSN Field: " << ssnField;
cout << "\n...length: " << ssnField.length() << endl; //not required by specs
editOK = editSSN(ssnField);
if(editOK)
{
cout << "...PASSED SSN EDIT\n";
}
else
cout << "...***FAILED SSN EDIT***\n";
}
else
cout << "...***FAILED NULL EDIT***\n";
return;
}
void test4()
{
cout << "\nTEST 4...\n\n"; //edit a user ID field
string userIDField; bool editOK;
const string EMPTY = ""; const int TRIM_CODE = 3;
cin.ignore(100,'\n');
cout << "Enter a user ID field to be tested: ";
getline(cin, userIDField);
if(userIDField != EMPTY)
{
cout << "...PASSED NULL EDIT\n";
trimSpace(userIDField,TRIM_CODE);
cout << "Trimmed user ID Field: " << userIDField;
cout << "\n...length: " << userIDField.length() << endl; //not required in specs
editOK = editID(userIDField);
if(editOK)
{
cout << "...PASSED USER ID EDIT\n";
}
else
cout << "...***FAILED USER ID EDIT***\n";
}
else
cout << "...***FAILED NULL EDIT***\n";
return;
}
void test5()
{
cout << "\nTEST 5...\n\n"; //edit a password field
string passwordField; bool editOK;
const string EMPTY = ""; const int TRIM_CODE = 3;
cin.ignore(100,'\n');
cout << "Enter a password field to be tested: ";
getline(cin, passwordField);
if(passwordField != EMPTY)
{
cout << "...PASSED NULL EDIT\n";
trimSpace(passwordField,TRIM_CODE);
cout << "Trimmed password Field: " << passwordField;
cout << "\n...length: " << passwordField.length() << endl; //not required in specs
editOK = editPasswrd(passwordField);
if(editOK)
{
cout << "...PASSED PASSWORD EDIT\n";
}
else
cout << "...***FAILED PASSWORD EDIT***\n";
}
else
cout << "...***FAILED NULL EDIT***\n";
return;
}
void test6()
{
cout << "\nTEST 6...\n\n";
return ;
}
void test7()
{
cout << "\nTEST 7...\n\n";
return;
}
bool editSSN(string)
{
cout << "editSSN() entered...\n";
if (string.length() != 11){ // test length
cout << "...SSN is too long or too short" << endl;
return false;
}
else if (string.find('-', 0) != 3){ //look for first hyphen
cout << "...incorret hyphen placement" << endl;
return false;
}
else if (string.find('-', 4) != 6){ //look for second hyphen
cout << "...incorret hyphen placement" << endl;
return false;
}
else if
return true;
}
bool editID(string)
{
cout << "editID() entered...\n";
for(int i = 0; i < string.length(); i++)
if( (string[i] >= '0' && string[i] <= '9') || (string[i] >= 'z' && string[i] >= 'a') || (string[i] <= 'Z' && string[i] >= 'A') )
cout << "...the ID must contain only alphanumeric characters" << endl;
return true;
else
return false;
}
bool editPasswrd(string)
{
cout << "editPasswrd() entered...\n";
if(string.length(); < 4 && string.length(); > 10){
cout << "...password must be more than 4 and less than 10 characters" << endl;
return false;
}
else if{
for(int i = 0; i < string.length(); i++)
if( (string[i] >= '0' && string[i] <= '9') || (string[i] >= 'z' && string[i] >= 'a') || (string[i] <= 'Z' && string[i] >= 'A') )
cout << "...the password must contain only alphanumeric characters" << endl;
return false
}
else if
return true;
}
int main()
{
int tstNum;
cout << "Enter Test Number (1-7): ";
cin >> tstNum;
while (tstNum)
{
if (tstNum == 1)
test1();
else if (tstNum == 2)
test2();
else if (tstNum == 3)
test3();
else if (tstNum == 4)
test4();
else if (tstNum == 5)
test5();
else if (tstNum == 6)
test6();
else if (tstNum == 7)
test7();
else
cout << "INVALID TEST NUMBER" << endl;
cout << endl << "Enter Test Number (1-7): ";
cin >> tstNum;
}
return 0;
}
[/cpp]
the part that i made is just from lines 151 to 200, i don't think i have to modify the other code that was already provided
here's the segment i made reposted so you can find it better or whatever
[cpp]
{
cout << "editSSN() entered...\n";
if (string.length() != 11){ // test length
cout << "...SSN is too long or too short" << endl;
return false;
}
else if (string.find('-', 0) != 3){ //look for first hyphen
cout << "...incorret hyphen placement" << endl;
return false;
}
else if (string.find('-', 4) != 6){ //look for second hyphen
cout << "...incorret hyphen placement" << endl;
return false;
}
else if
return true;
}
bool editID(string)
{
cout << "editID() entered...\n";
for(int i = 0; i < string.length(); i++)
if( (string[i] >= '0' && string[i] <= '9') || (string[i] >= 'z' && string[i] >= 'a') || (string[i] <= 'Z' && string[i] >= 'A') )
cout << "...the ID must contain only alphanumeric characters" << endl;
return true;
else
return false;
}
bool editPasswrd(string)
{
cout << "editPasswrd() entered...\n";
if(string.length(); < 4 && string.length(); > 10){
cout << "...password must be more than 4 and less than 10 characters" << endl;
return false;
}
else if{
for(int i = 0; i < string.length(); i++)
if( (string[i] >= '0' && string[i] <= '9') || (string[i] >= 'z' && string[i] >= 'a') || (string[i] <= 'Z' && string[i] >= 'A') )
cout << "...the password must contain only alphanumeric characters" << endl;
return false
}
else if
return true;
}
[/cpp]
here is what each of those 3 functions need to do:
"bool editSSN(string)" is to make sure that if the user enters a social security number and that the number is exactly in the format of 000-00-0000, as in it has the exactly 9 numbers, it has only 2 hyphens in the right spots, and that it has only numbers and no letters
"bool editID(string)" is to check and see if the user inputs a 'user id number' that contains only numbers and letters. (no special characters such as #*%^)
and lastly "bool editPasswrd(string)" is to check a user's password to see that its between 4 and 10 characters long and that it only contains alphanumeric characters just like in the 'editID' function above.
the 3 functions were completely empty at the start of the assignment and i feel like i'm done but i get compiler errors and i don't know whats wrong.
here's the error's that visual studio tells me (don't worry most of these are repeat errors since it looks like i'm making the same mistakes over and over again)
the 3 digit number to the right is the line number where the error is
Error 1 error C2059: syntax error : '.' 156
Error 2 error C2143: syntax error : missing ';' before '{' 156
Error 3 error C2181: illegal else without matching if 160
Error 4 error C2059: syntax error : '.' 160
Error 5 error C2143: syntax error : missing ';' before '{' 160
Error 6 error C2181: illegal else without matching if 164
Error 7 error C2059: syntax error : '.' 164
Error 8 error C2143: syntax error : missing ';' before '{' 164
Error 9 error C2181: illegal else without matching if 169
Error 10 error C2059: syntax error : 'return' 170
Error 11 error C2275: 'std::string' : illegal use of this type as an expression 176
Error 12 error C2228: left of '.length' must have class/struct/union 176
Error 13 error C2143: syntax error : missing ')' before '>=' 177
Error 14 error C2540: non-constant expression as array bound 177
Error 15 error C2059: syntax error : '>=' 177
Error 16 error C2059: syntax error : ')' 177
Error 17 error C2181: illegal else without matching if 180
Error 18 error C2059: syntax error : '.' 187
Error 19 error C2143: syntax error : missing ';' before '<' 187
Error 20 error C2143: syntax error : missing ';' before '>' 187
Error 21 error C2143: syntax error : missing ';' before '{' 187
Error 22 error C2181: illegal else without matching if 191
Error 23 error C2059: syntax error : '{' 191
Error 24 error C2143: syntax error : missing ';' before '{' 191
Error 25 error C2275: 'std::string' : illegal use of this type as an expression 192
Error 26 error C2228: left of '.length' must have class/struct/union 192
Error 27 error C2143: syntax error : missing ')' before '>=' 193
Error 28 error C2540: non-constant expression as array bound 193
Error 29 error C2059: syntax error : '>=' 193
Error 30 error C2059: syntax error : ')' 193
Error 31 error C2143: syntax error : missing ';' before '}' 196
Error 32 error C2181: illegal else without matching if 198
Error 33 error C2059: syntax error : 'return' 199
34 IntelliSense: a nonstatic member reference must be relative to a specific object 156
35 IntelliSense: a nonstatic member reference must be relative to a specific object 160
36 IntelliSense: a nonstatic member reference must be relative to a specific object 164
37 IntelliSense: expected a '(' 170
38 IntelliSense: expected an expression 171
39 IntelliSense: a nonstatic member reference must be relative to a specific object 176
40 IntelliSense: type name is not allowed 177
41 IntelliSense: type name is not allowed 177
42 IntelliSense: type name is not allowed 177
43 IntelliSense: type name is not allowed 177
44 IntelliSense: type name is not allowed 177
45 IntelliSense: type name is not allowed 177
46 IntelliSense: expected a statement 180
47 IntelliSense: a nonstatic member reference must be relative to a specific object 187
48 IntelliSense: expected a ')' 187
49 IntelliSense: expected a '(' 191
50 IntelliSense: expected an expression 200
In your actual function definitions, you have to provide the name and type for each argument:
[code]
bool editID(string)
{
...
should be:
bool editID(string str)
{
...
//(you'll have to change the code that uses the 'string' variable to use 'str' instead)
[/code]
I'm having a bit of a conceptual problem regarding hash tables. I want to use a hash table like a database, i.e. the key will be the primary key and the value will be a struct that holds the whole record. But I want the key to also be part of the record. Should I also store it in the value struct or is there another way to reference it? (Bidirectional hash wouldn't work)
[QUOTE=ThePuska;38394914]But your implementation of it is getting even more results than it should.[/quote]
but there are 4x3x2x1 = 24 outcomes
[QUOTE=swift and shift;38402832]but there are 4x3x2x1 = 24 outcomes[/QUOTE]
Yes, but Dajoh's method can only give 4.
[QUOTE=ThePuska;38406156]Yes, but Dajoh's method can only give 4.[/QUOTE]
but it gave 16
Remove the matrix transpositions and it won't.
-snip- figured it out
Snip, Nevermind!
you never set list to anything
[QUOTE=swift and shift;38414511]you never set list to anything[/QUOTE]
Yep completely overlooked that and I felt stupid. forgot to edit my post, my bad
I am working with the Android NDK and I have come across a really, really wierd issue.
I am trying to read data from a char array with the following code:
[code]float ReadFloat()
{
float value;
float *sp = (float *)&this->data[this->readpos];
value = *sp;
this->readpos += 4;
return value;
};[/code]
This code works perfectly fine on PC, but it crashes in Android with a "Incorrect access to memory (data misalignment)" Exception on the line "value = *sp;"
However, this code works perfectly fine with no exception.
[code]int ReadInt()
{
int value;
int *sp = (int *)&this->data[this->readpos];
value = *sp;
this->readpos += 4;
return value;
};[/code]
Does anyone know whats going on here? I sure don't.
[editline]11th November 2012[/editline]
Ok, I did a bit more research and I found that ARM does not allow you to use floats that are not properly aligned. The float I was using was offset by 1 byte in the array, so it threw an error when I tried to dereference it.
I just used memcpy and now it works fine.
the proper solution would be to make sure 'data' is aligned
Anyone have a good source on learning big O notation (I think that's what I'm looking for). I'm trying to solve a problem and it says that it should be N*(log(N)) or faster. I've never really been taught this stuff, but I get the basic ones like o(n^2) I think.
edit: It's prime number finder, I've made one before but I don't know if it's N(log(n)) or not because I don't even know what that is.
Implementing SHA1 in C#, this is just a rough draft of it:
[cpp]
using System;
using System.Linq;
namespace HashFunctions
{
public class Sha1
{
private struct Hash
{
public uint A, B, C, D, E;
}
private static readonly Hash Seed = new Hash
{
A = 0x67452301,
B = 0xEFCDAB89,
C = 0x98BADCFE,
D = 0x10325476,
E = 0xC3D2E1F0,
};
public byte[] Digest (byte[] key)
{
key = preProcess (key);
byte[][] chunks = split (key, 64);
Hash hash = chunks.Aggregate (Seed, compress);
return hashToByteArray (hash);
}
private static byte[] preProcess (byte[] key)
{
long keyLength = key.LongLength;
long newLength = key.LongLength;
newLength += 9;
newLength += 64 - (newLength % 64);
var newKey = new byte[newLength];
Array.Copy (key, newKey, keyLength);
key = newKey;
long i = keyLength;
key [i++] = 1 << 7;
while (i < newLength - 8) {
key [i++] = 0;
}
byte[] lengthData = BitConverter.GetBytes (keyLength * 8);
if (BitConverter.IsLittleEndian) {
Array.Reverse (lengthData);
}
Array.Copy (lengthData, 0, key, i, lengthData.LongLength);
return key;
}
private static byte[][] split (byte[] key, int chunkSize)
{
var chunks = new byte[key.LongLength / chunkSize][];
for (var i = 0; i < chunks.Length; i++) {
chunks [i] = new byte[chunkSize];
Array.Copy (key, chunkSize * i, chunks [i], 0, chunkSize);
}
return chunks;
}
private static Hash compress (Hash accumulator, byte[] chunk)
{
var w = new uint[80];
for (var i = 0; i < 16; i++) {
if (BitConverter.IsLittleEndian) {
Array.Reverse (chunk);
}
w [i] = BitConverter.ToUInt32 (chunk, i * 4);
}
for (var i = 16; i < 80; i++) {
w [i] = rotateLeft (w [i - 3] ^ w [i - 8] ^ w [i - 14] ^ w [i - 16], 1);
}
uint a = accumulator.A,
b = accumulator.B,
c = accumulator.C,
d = accumulator.D,
e = accumulator.E;
for (var i = 0; i < 80; i++) {
uint f = getF (b, c, d, i);
uint k = getK (i);
uint temp = rotateLeft (a, 5) + f + e + k + w [i];
e = d;
d = c;
c = rotateLeft (b, 30);
b = a;
a = temp;
}
accumulator.A += a;
accumulator.B += b;
accumulator.C += c;
accumulator.D += d;
accumulator.E += e;
return accumulator;
}
private static uint rotateLeft (uint x, int steps)
{
return x << steps | x >> (sizeof(uint) * 8 - steps);
}
private static uint getF (uint b, uint c, uint d, int i)
{
if (i < 20) {
return (b & c) | (~b & d);
} else if (i < 40) {
return b ^ c ^ d;
} else if (i < 60) {
return (b & c) | (b & d) | (c & d);
} else {
return b ^ c ^ d;
}
}
private static uint getK (int i)
{
if (i < 20) {
return 0x5A827999;
} else if (i < 40) {
return 0x6ED9EBA1;
} else if (i < 60) {
return 0x8F1BBCDC;
} else {
return 0xCA62C1D6;
}
}
private static byte[] hashToByteArray (Hash hash)
{
var digest = new byte[20];
Array.Copy (BitConverter.GetBytes (hash.A), 0, digest, 0, 4);
Array.Copy (BitConverter.GetBytes (hash.B), 0, digest, 4, 4);
Array.Copy (BitConverter.GetBytes (hash.C), 0, digest, 8, 4);
Array.Copy (BitConverter.GetBytes (hash.D), 0, digest, 12, 4);
Array.Copy (BitConverter.GetBytes (hash.E), 0, digest, 16, 4);
return digest;
}
}
}
[/cpp]
It seems to work incorrectly. If I give it an empty byte array, it should give me the hash da39a3ee5e6b4b0d3255bfef95601890afd80709, but it gives me e504b492ed8c58564ecd1a6c683f05bf933af709. Anyone knowledgeable about this kind of stuff here that can see what I do wrong in my implementation? I myself guess that it has something to do with endianness, but I have tried with different conversions and stuff, and I didn't get the correct answer.
Edit: Never mind, I fixed it. I'm going to leave my problem here and write out the solution anyway.
This line:
[cpp]
Array.Reverse(chunk);
[/cpp]
Was supposed to just reverse the 4-byte subchunk being converted to a UInt32, but instead it reversed the whole 64-byte chunk. I changed it to:
[cpp]
Array.Reverse(chunk, i * 4, 4);
[/cpp]
I also had to reverse the byte arrays converted from 4-byte words h0, h1, h2, h3 and h4 before concatenating them. After that it worked perfectly.
Please don't implement SHA1 in C# for non-educational purposes. Use a library.
[QUOTE=Jookia;38419693]Please don't implement SHA1 in C# for non-educational purposes. Use a library.[/QUOTE]
It's for educational purposes :v:
I think .Net even has an implementation built-in.
[QUOTE=ArgvCompany;38419738]It's for educational purposes :v:
I think .Net even has an implementation built-in.[/QUOTE]
[url=http://msdn.microsoft.com/en-us/library/system.security.cryptography.sha1.aspx]It does[/url]
[QUOTE=Shadaez;38418513]Anyone have a good source on learning big O notation (I think that's what I'm looking for). I'm trying to solve a problem and it says that it should be N*(log(N)) or faster. I've never really been taught this stuff, but I get the basic ones like o(n^2) I think.
edit: It's prime number finder, I've made one before but I don't know if it's N(log(n)) or not because I don't even know what that is.[/QUOTE]
I'm not sure how you can understand O(n²) but not O(n log n)? How would you describe what you think O(n²) is?
[QUOTE=ArgvCompany;38419571]Implementing SHA1 in C#, this is just a rough draft of it:[/cpp]
It seems to work incorrectly. If I give it an empty byte array, it should give me the hash da39a3ee5e6b4b0d3255bfef95601890afd80709, but it gives me e504b492ed8c58564ecd1a6c683f05bf933af709. Anyone knowledgeable about this kind of stuff here that can see what I do wrong in my implementation? I myself guess that it has something to do with endianness, but I have tried with different conversions and stuff, and I didn't get the correct answer.
Edit: Never mind, I fixed it. I'm going to leave my problem here and write out the solution anyway.
This line:
[cpp]
Array.Reverse(chunk);
[/cpp]
Was supposed to just reverse the 4-byte subchunk being converted to a UInt32, but instead it reversed the whole 64-byte chunk. I changed it to:
[cpp]
Array.Reverse(chunk, i * 4, 4);
[/cpp]
I also had to reverse the byte arrays converted from 4-byte words h0, h1, h2, h3 and h4 before concatenating them. After that it worked perfectly.[/QUOTE]
The hash function only requires 64 bytes of the message at a time. Maybe you could change it to chew on a chunk instead of requiring the entire message.
[cpp]hash.Start();
hash.Feed(some_data);
// Maybe I'll do some other things here before I get the rest of the message
hash.Feed(rest_of_the_data);
byte[] result = hash.End();[/cpp]
[QUOTE=ThePuska;38420325]The hash function only requires 64 bytes of the message at a time. Maybe you could change it to chew on a chunk instead of requiring the entire message.
[cpp]hash.Start();
hash.Feed(some_data);
// Maybe I'll do some other things here before I get the rest of the message
hash.Feed(rest_of_the_data);
byte[] result = hash.End();[/cpp][/QUOTE]
Actually that would be a pretty good idea, but I'm not sure how it would be useful in my program (which is supposed to read a file and write its digest to another file).
Files are streams. You could process them while reading and be done immediately when the entire file is read
[QUOTE=ThePuska;38420404]Files are streams. You could process them while reading and be done immediately when the entire file is read[/QUOTE]
Yes, but for that to work in C# I'd have to bother with multithreading, not worth it :v:
[QUOTE=ArgvCompany;38420410]Yes, but for that to work in C# I'd have to bother with multithreading, not worth it :v:[/QUOTE]
Why would you need multihreading?
How would I go about moving an object to a specific spot on the screen? In my entity, I have a position Vector2f, and a target Vector2f. I can't seem to find anything on the internet that helps me out with this.
EDIT:
Figured out how to do it, this is what I am doing:
direction.set((target.x - position.x),(target.y - position.y));
But when the entity gets close to the target, he starts slowing down. Is there a way to avoid this? I want him to go to the position at a constant speed. This is how I am moving the ent:
position.x += (direction.x * speed);
position.y += (direction.y * speed);
speed is .01f.
[QUOTE=Richy19;38420556]Why would you need multihreading?[/QUOTE]
Wouldn't the point be to digest and write a chunk at the same time as reading the next one? To do two things at a time you would need threads, or else there would be no point of reading the file in chunks.
[QUOTE=ArgvCompany;38420576]Wouldn't the point be to digest and write a chunk at the same time as reading the next one? To do two things at a time you would need threads, or else there would be no point of reading the file in chunks.[/QUOTE]
the idea of reading the file in chunks is so you can calculate the sha1 sum of a 20 gb file on a computer with 4 gb of memory
[QUOTE=swift and shift;38420738]the idea of reading the file in chunks is so you can calculate the sha1 sum of a 20 gb file on a computer with 4 gb of memory[/QUOTE]
Yeah, that would be a valid point, I thought of that too. I guess I misunderstood what ThePuska meant.
Sorry, you need to Log In to post a reply to this thread.