The "IT DOESN'T WORK!" thread

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

Re: The "IT DOESN'T WORK!" thread

Postby mr-mitch » Tue Jan 25, 2011 11:43 am UTC

Thank you. I suppose "" is the 'null string'.

I looked up the PHP manual entry for strings after reading your post and it states this:

NULL is always converted to an empty string.


So that explains why it wasn't behaving as I expected.

I think it's a little misleading that isset() states 'is set and is not NULL' while NULL is "" as a string.
mr-mitch
 
Posts: 450
Joined: Sun Jul 05, 2009 6:56 pm UTC

Re: The "IT DOESN'T WORK!" thread

Postby LDJ » Tue Feb 15, 2011 10:09 am UTC

Hi guys,
For awhile, I've had a Perl xkcd downloader script that I've been using, to save all the comics+ALT text to my computer, and, until recently, it worked. It had been put together from an old script I found on the net+ some help from people, etc.

But, for the life of me, I cannot figure out why it will no longer work.
The problem lies in that, the script SHOULD (and did), find the file on the server for the comic, and save the file, with whatever extension it had. (Most are PNG, but some are JPG)

But now, all files are being saved without an extension, e.g. "MyFile.", instead of "MyFile.png"


I know it's a stretch, but could anyone shed some light on to what the problem might be?

Thanks so much for your help,
Liam.
LDJ
 
Posts: 5
Joined: Wed Jan 26, 2011 5:11 am UTC

Re: The "IT DOESN'T WORK!" thread

Postby phlip » Tue Feb 15, 2011 11:32 am UTC

It would be easier if you posted the relevant bit of your code...

Also, I hope you're using the JSON and not just trying to scrape the HTML...
While no one overhear you quickly tell me not cow cow.
but how about watch phone?
User avatar
phlip
Restorer of Worlds
 
Posts: 6731
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia

Re: The "IT DOESN'T WORK!" thread

Postby LDJ » Wed Feb 16, 2011 6:30 am UTC

Sorry, I wasn't thinking. Also, I've fixed the issue, I had been declaring a variable fairly late in the code, and should have done so at the beginning. After fixing that, it works again.
I've attached the code in case anyone else would like to use this.

Also, I'm sorry, but I'm not using JSON, I have absolutely NO experience or knowledge as to how to use it.

But if anyone wants to expand on this, and incorporate JSON, feel free.

Thanks,
Liam.

xkcd Perl Downloader:
Spoiler:
Code: Select all
#!/usr/bin/perl

use LWP::Simple;
#  use Smart::Comments;

## Objectives ##

#  Download all comics from xkcd.com
#  Ability to download new comics
#  Download ALT text
#  Saved in: ~/Desktop

## Bugs ##

# Files that aren't a png, are being saved as one.
my $ext; #Declare variable to store file extension until file is saved.

# Set Specifics
# etc.
#.
$sitePrefix = "http://xkcd.com/";

## Path to main xkcd directory ##
$path = "$ENV{HOME}/Desktop";


mkdir "$path/xkcd", 0755 or print "xkcd Directory Exists\n";
chomp($path = "$path/xkcd");

$d = get($sitePrefix);
if ($d =~ /http:\/\/xkcd.com\/(\d+)\//) {
    $current = $1;
}

# Obtains all individual comic data
sub getComicData {
    my $siteData = get("$sitePrefix$current/");
    my @data = split /\n/, $siteData;
    foreach (@data) {
        if (/http:\/\/xkcd.com\/(\d+)\//) {
            $current = $1;
        }
        if (/src="(http:\/\/imgs.xkcd.com\/comics\/.+(\.\w{3}))"/) {
            $currentUrl = $1;
            $ext = $2;
            sleep(3);
            if (/alt="(.+?)"/) {
                $title = $1;
                $title =~ s/&#(\d+);/chr($1)/ge;
            $title = "House of Pancakes" if $current == 472;  # Color title on comic 472 with weird syntax
            }
            if (/title="(.+?)"/) {    #title commonly know as 'alt' text
                $alt = $1;
                $alt =~ s/&#(\d+);/chr($1)/ge;
            }
        }
    }
}

chdir "$path" or die "Cannot change directory: $!";
&getComicData();
while ( get("$sitePrefix$current/")){ ### Writing Files $current: $title
    print "Writing Files $current: $title\n";
   # Create directories for individual comics
    mkdir "$current $title", 0755 or die "Previously Downloaded";
    chdir "$path/$current $title" or die "Cannot change directory: $!";

    # Save image file
    $image = get($currentUrl);
    open my $IMAGE, '>>', "$title$ext"
        or die "Cannot create file!";
    print $IMAGE $image;
    close $IMAGE;

    # Save alt text
    open my $TXT, '>>', "$title ALT.txt"
        or die "Cannot create file!";
    print $TXT $alt;
    close $TXT;
    chdir "$path" or die "Cannot change directory: $!";
    $current--;

    # Check for non existent 404 comic
    $current-- if $current == 404;

    &getComicData();
}


# End Gracefully
print "Download Complete\n"
LDJ
 
Posts: 5
Joined: Wed Jan 26, 2011 5:11 am UTC

Re: The "IT DOESN'T WORK!" thread

Postby Yakk » Wed Feb 16, 2011 3:37 pm UTC

Google xkcd json gets me this thread:
viewtopic.php?f=11&t=3095&p=1716879
Following the link in that thread:
http://xkcd.com/432/info.0.json
gets me this in a text editor:
Code: Select all
{"img": "http://imgs.xkcd.com/comics/journal_4.png", "title": "Journal 4", "month": "6", "num": 432, "link": "", "year": "2008", "news": "XKCD updates every Monday, Wednesday, and Friday.<br />Previously: <a href=\"http://xkcd.com/374/\">Journal</a>", "safe_title": "Journal 4", "transcript": "[[The man with the hat sits slumped over on a bench, holding his hat]]\nMan with hat: Sigh\n{{alt text: Man, this emo shit was supposed to be for people who didn't have hats.}}", "alt": "Man, this emo shit was supposed to be for people who didn't have hats.", "day": "4"}

which looks utterly trivial to parse, even if one knew nothing about JSON. I might have to spend a short bit learning how JSON escapes things in their quotes (probably backslash based?), and then I could write a five-line perl function to load that into a map (decoded), and the rest of the program becomes trivial.
One of the painful things about our time is that those who feel certainty are stupid, and those with any imagination and understanding are filled with doubt and indecision - BR

Last edited by JHVH on Fri Oct 23, 4004 BCE 6:17 pm, edited 6 times in total.
User avatar
Yakk
 
Posts: 10036
Joined: Sat Jan 27, 2007 7:27 pm UTC
Location: E pur si muove

Re: The "IT DOESN'T WORK!" thread

Postby headprogrammingczar » Wed Feb 16, 2011 5:03 pm UTC

<quintopia> You're not crazy. you're the goddamn headprogrammingspock!
<Weeks> You're the goddamn headprogrammingspock!
<Cheese> I love you
User avatar
headprogrammingczar
 
Posts: 2953
Joined: Mon Oct 22, 2007 5:28 pm UTC
Location: Beaming you up

Re: The "IT DOESN'T WORK!" thread

Postby Windowlicker » Thu Feb 17, 2011 8:19 pm UTC

Code: Select all
board = new ArrayList<Integer>[31][31];

Error: Cannot create a generic array of ArrayList<Integer>

I wonder why not.. works without the "<Integer>" well enough, and I can sure enough declare "ArrayList<Integer>[][] board;"...
Anyone any ideas why this happens? Like I say the code works fine without it, it's just giving annoying warnings about it.
Windowlicker
 
Posts: 323
Joined: Wed Dec 23, 2009 6:57 pm UTC
Location: St Andrews, Scotland

Re: The "IT DOESN'T WORK!" thread

Postby headprogrammingczar » Fri Feb 18, 2011 12:19 am UTC

Java can't make arrays of generic types for some bullshit reason. Apparently they expect type safety from generics where there wasn't any for arrays to begin with. Example below:
Spoiler:
Code: Select all
Integer[] test = new Integer[100];
Number[] oops = test;
oops[0] = new Float(3.1415);


Code: Select all
List<Integer> li = new ArrayList<Integer>();
List<Number> ln = li; // illegal
ln.add(new Float(3.1415));
<quintopia> You're not crazy. you're the goddamn headprogrammingspock!
<Weeks> You're the goddamn headprogrammingspock!
<Cheese> I love you
User avatar
headprogrammingczar
 
Posts: 2953
Joined: Mon Oct 22, 2007 5:28 pm UTC
Location: Beaming you up

Re: The "IT DOESN'T WORK!" thread

Postby Rippy » Sat Feb 19, 2011 11:14 pm UTC

I'm a bit confused with Haskell type vs value constructors. For example, say I have a type Bar which can be a Foo or an Int:
Code: Select all
data Foo = Foo Int Int
data Bar = Int | Foo

That won't run, citing "multiple declaration of Foo". If you do this, however:
Code: Select all
data Foo = NewFoo Int Int
data Bar = Int | Foo

It runs just fine. It was my understanding, though, that because the situations in which type/value constructors are used, the compiler can always differentiate them, so it's no big deal giving them the same name. It seems awfully arbitrary to have to change the value constructor to something different for just this one type. Surely there's some way of doing this while keeping the same name for the type/value constructors?
User avatar
Rippy
 
Posts: 2103
Joined: Sun Jul 22, 2007 11:27 pm UTC
Location: Ontario, Can o' Duh

Re: The "IT DOESN'T WORK!" thread

Postby headprogrammingczar » Sun Feb 20, 2011 12:06 am UTC

Right, so what's happening there is a misunderstanding regarding constructors.

When you define a data type in Haskell, the syntax is "data <type> = <constructor> <types of args> | <more constructors...>
What you did in the first is say, "one type Foo has a constructor Foo :: Int -> Int -> Foo. Bar has two constructors; Int :: Bar, and Foo :: Bar". You can get away with Int as a constructor, for the most unintuitive reason imaginable:
Code: Select all
data Int = I# Int#

This is the GHC source for the Int type. There's no Int constructor, so you are free to make Int :: Bar. There is a Foo constructor though, and it is of type Int -> Int -> Foo. When you declare another one, Foo :: Bar, you are contradicting yourself. What you want is not the first or the second, but something like this:
Code: Select all
data Foo = Foo Int Int
data Bar = IVal Int | FooVal Foo


Now you can make a Bar from two constructors: Ival :: Int -> Bar, and FooVal :: Foo -> Bar.
<quintopia> You're not crazy. you're the goddamn headprogrammingspock!
<Weeks> You're the goddamn headprogrammingspock!
<Cheese> I love you
User avatar
headprogrammingczar
 
Posts: 2953
Joined: Mon Oct 22, 2007 5:28 pm UTC
Location: Beaming you up

Re: The "IT DOESN'T WORK!" thread

Postby Rippy » Sun Feb 20, 2011 12:57 am UTC

That makes sense. Has anybody told you lately that you are awesome?
User avatar
Rippy
 
Posts: 2103
Joined: Sun Jul 22, 2007 11:27 pm UTC
Location: Ontario, Can o' Duh

Re: The "IT DOESN'T WORK!" thread

Postby llamanaru » Tue Feb 22, 2011 12:19 pm UTC

I'm working on an SQL project using python. Can anyone see what's wrong with this statement?
Code: Select all
create table PUBACC_CD
(
   Record_Type         char(2)      null,
   Unique_System_Identifier   numeric(9,0)   not null,
   ULS_File_Number         char(14)   null,
   EBF_Number         varchar(30)   null,
   Year_Sequence         smallint   not null,
   Gross_Revenues         money      null,
   Year_End_Date         datetime   null,
   Aggregate_Gros_Rvn_DE      money      null,
   Aggregate_Gros_Rvn_CB      money      null,
   Total_Assets         money      null         
)

Error is specifically
Code: Select all
exceptions.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'money\t\tnull,\n\tYear_End_Date\t\t\tdatetime\tnull,\n\tAggregate_Gros_Rvn_DE\t\tmoney\t\tnull' at line 8")


Thanks for any help you can give me.
User avatar
llamanaru
 
Posts: 241
Joined: Sat May 01, 2010 2:40 am UTC
Location: Colorado

Re: The "IT DOESN'T WORK!" thread

Postby phlip » Tue Feb 22, 2011 12:25 pm UTC

There isn't a datatype called "MONEY", which is where it's falling down. You want one of the numeric types that exist, probably something like DECIMAL(10,2).
While no one overhear you quickly tell me not cow cow.
but how about watch phone?
User avatar
phlip
Restorer of Worlds
 
Posts: 6731
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia

Re: The "IT DOESN'T WORK!" thread

Postby llamanaru » Tue Feb 22, 2011 11:52 pm UTC

That would be a problem. It never occurred to me that they'd give me bad SQL definitions... Thank you very much.
User avatar
llamanaru
 
Posts: 241
Joined: Sat May 01, 2010 2:40 am UTC
Location: Colorado

Re: The "IT DOESN'T WORK!" thread

Postby phlip » Wed Feb 23, 2011 12:16 am UTC

Apparently there is a type called "MONEY" in MSSQL... it isn't standard, though. Maybe the notes have been ported from one DBMS to another and they forgot to change it? And didn't test it?
While no one overhear you quickly tell me not cow cow.
but how about watch phone?
User avatar
phlip
Restorer of Worlds
 
Posts: 6731
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia

Re: The "IT DOESN'T WORK!" thread

Postby llamanaru » Wed Feb 23, 2011 12:36 am UTC

That's probably it. The project is to translate some FCC database management stuff originally written in Visual BASIC and MS Access into something more scalable. So it's true, they've been ported and they I forgot to change it and didn't test it. Thanks again.
User avatar
llamanaru
 
Posts: 241
Joined: Sat May 01, 2010 2:40 am UTC
Location: Colorado

Re: The "IT DOESN'T WORK!" thread

Postby Rippy » Sun Feb 27, 2011 9:04 pm UTC

I'm afraid I'm having some trouble figuring out State/Reader stuff in Haskell. Wondering if anyone can help me figure out why the following doesn't work:

Spoiler:
Code: Select all
type TileArray = IOArray (Int,Int) Tile

data AppData = AppData {
    fps :: Timer,
    tiles :: TileArray
}

data AppConfig = AppConfig {
    screen :: Surface,
    tileSheet :: Surface
}

getTiles :: MonadState AppData m => m TileArray
getTiles = liftM tiles get

showTile :: (MonadIO m, MonadState AppData m) => Int -> Int -> m ()
showTile x y = do
    tiles <- getTiles
    tile <- readArray tiles (x,y)           --Problem happens here
    let tileIndex = fromEnum $ terrain tile
    screen <- getScreen
    tileSheet <- getTileSheet
    applySurface' x y tileSheet screen $ Just $ clips !! tileIndex
    return ()

I'm trying to pull out the IOArray from the state so that I can read an element from it. However, it gives this error for the line containing readArray:

Spoiler:
Code: Select all
Evsim.hs:97:12:
    Could not deduce (MArray GHC.IOArray.IOArray Tile m)
      from the context (MonadState AppData m)
      arising from a use of `readArray' at Evsim.hs:97:12-32
    Possible fix:
      add (MArray GHC.IOArray.IOArray Tile m) to the context of
        the type signature for `showTile'
      or add an instance declaration for
         (MArray GHC.IOArray.IOArray Tile m)
    In a stmt of a 'do' expression: tile <- readArray tiles (x, y)
    In the expression:
        do { tiles <- getTiles;
             tile <- readArray tiles (x, y);
             let tileIndex = fromEnum $ terrain tile;
             screen <- getScreen;
             .... }
    In the definition of `showTile':
        showTile x y
                   = do { tiles <- getTiles;
                          tile <- readArray tiles (x, y);
                          let tileIndex = ...;
                          .... }


I'm reworking a function obtained from the Lazy Foo SDL tutorials, which originally looked like this:

Spoiler:
Code: Select all
showTile :: (MonadIO m, MonadReader AppConfig m) => Camera -> Tile -> m ()
showTile camera@(Rect cx cy _ _) (Tile tbox@(Rect tx ty _ _) tileType) =
    when (intersects tbox camera) $ do
        screen <- getScreen
        tileSheet <- getTileSheet
        applySurface' (tx - cx) (ty - cy) tileSheet screen $ Just $ clips !! tileIndex
        return ()
 where tileIndex = fromEnum tileType

So originally it only grabbed info from the Reader part (AppConfig); "camera" was part of AppData, but in this case it's passed as an argument. I can't afford to pass the entire array around repeatedly. Now I need information from both AppConfig and AppData, and I can't find any examples to help figure it out. If anyone can see what it is I'm not getting, I'd really appreciate it (I understand I'm being pretty vague). In the meantime I'm reading up more on State and Reader.
User avatar
Rippy
 
Posts: 2103
Joined: Sun Jul 22, 2007 11:27 pm UTC
Location: Ontario, Can o' Duh

Re: The "IT DOESN'T WORK!" thread

Postby headprogrammingczar » Sun Feb 27, 2011 11:35 pm UTC

Rippy wrote:
Code: Select all
    Possible fix:
      add (MArray GHC.IOArray.IOArray Tile m) to the context of
        the type signature for `showTile'
      or add an instance declaration for
         (MArray GHC.IOArray.IOArray Tile m)

This is the fix. Your showTile function doesn't have a class constraint it needs, which it mentions above. Add (MArray IOArray Tile m) to the context of the type signature for showTile.
Code: Select all
showTile :: (MonadIO m, MonadState AppData m) => Int -> Int -> m ()
            |-------------------------------|
                   This is the context

-- what you want
showTile :: (MonadIO m, MonadState AppData m, MArray IOArray Tile m) => Int -> Int -> m ()


Learning the GHC errors takes time, but the errors are always exactly what they say, in my experience.
<quintopia> You're not crazy. you're the goddamn headprogrammingspock!
<Weeks> You're the goddamn headprogrammingspock!
<Cheese> I love you
User avatar
headprogrammingczar
 
Posts: 2953
Joined: Mon Oct 22, 2007 5:28 pm UTC
Location: Beaming you up

Re: The "IT DOESN'T WORK!" thread

Postby Rippy » Mon Feb 28, 2011 12:25 am UTC

yyyyyep that did it (along with a few other slight tweaks).

The problem, honestly, is that I don't quite understand the code. So far I'm finding that it's not so much that Haskell is bad for doing graphics, but that you need a much, much deeper understanding of the language than you do for a comparable graphical app/game in C++ in order to do it.

My attitude right now is to just get the graphics bits to work, even if I don't understand it, so that I can work on the pure code which I do understand for now, and figure out the rest later. There's only so much reading you can do before you have to put something into practice before moving forward.

So, that said, I appreciate the patience!
User avatar
Rippy
 
Posts: 2103
Joined: Sun Jul 22, 2007 11:27 pm UTC
Location: Ontario, Can o' Duh

Re: The "IT DOESN'T WORK!" thread

Postby headprogrammingczar » Mon Feb 28, 2011 1:04 pm UTC

Yeah, all the graphics shit is pretty much its own language anyway, so learning an unfamiliar language in another unfamiliar language isn't going to be very easy. You seem to be having better luck than I did though.
<quintopia> You're not crazy. you're the goddamn headprogrammingspock!
<Weeks> You're the goddamn headprogrammingspock!
<Cheese> I love you
User avatar
headprogrammingczar
 
Posts: 2953
Joined: Mon Oct 22, 2007 5:28 pm UTC
Location: Beaming you up

Re: The "IT DOESN'T WORK!" thread

Postby Rippy » Tue Mar 01, 2011 2:36 pm UTC

Eh, I am somewhat stuck again. The main program loop complains when it tries to call my showTile function (the one I added the IOArray class constraint to). loop is of type "ReaderT AppConfig (StateT AppData IO)", and though I forget the exact error, I imagine that's the problem somehow.

I have caved and started reading again though, and I've gotten to monad transformers, which is helping tremendously with understanding what's going on.
User avatar
Rippy
 
Posts: 2103
Joined: Sun Jul 22, 2007 11:27 pm UTC
Location: Ontario, Can o' Duh

Re: The "IT DOESN'T WORK!" thread

Postby headprogrammingczar » Tue Mar 01, 2011 6:44 pm UTC

Transformers are awesome. Not only do they give you the functionality of multiple monads at once, but they also turn into sweet cars.

(More seriously, I suggest looking at the source code for some simple transformers, along with their MonadTrans instance (iirc) to see what lift does)
<quintopia> You're not crazy. you're the goddamn headprogrammingspock!
<Weeks> You're the goddamn headprogrammingspock!
<Cheese> I love you
User avatar
headprogrammingczar
 
Posts: 2953
Joined: Mon Oct 22, 2007 5:28 pm UTC
Location: Beaming you up

Re: The "IT DOESN'T WORK!" thread

Postby tipo test » Mon Mar 07, 2011 6:41 pm UTC

MySQL, silly example database:

Code: Select all
create table fruit(id varchar(34));

insert into fruit values (1), (2), (3), (4);

create table person(id varchar(34));

insert into person values (1), (2);

create table eats(fruit varchar(34), person varchar(34));

insert into eats values (1, 1), (2, 1), (3, 1), (4, 1), (1, 2); /* Only person #1 has eaten ALL of the fruits */


I need a query that retrieves all the persons who have eaten all the different kind of fruits . I guess I'd have to use count() and a subquery but It's not working the way I expect...
User avatar
tipo test
 
Posts: 71
Joined: Sun May 23, 2010 12:15 pm UTC

Re: The "IT DOESN'T WORK!" thread

Postby phlip » Mon Mar 07, 2011 11:35 pm UTC

I can see two options, neither particularly scalable... the direct approach:
Code: Select all
select id from person where not exists(select 1 from fruit where not exists(select 1 from eats where eats.fruit=fruit.id and eats.person = person.id));
Includes unindexed queries on both person and fruit, but should always work.
Alternatively, the counting approach:
Code: Select all
select person from eats group by person having count(distinct fruit) = (select count(*) from fruit);
Contains an undexed query on eats, and also will only work if there every value in eats.fruit is in fruit.id... ie there aren't any dangling references. Which I think is one of the many consistency checks that MyISAM won't enforce. So be careful with this one. This could possibly be solved by adding a where exists() clause, or an inner join to fruit, to filter out any rows with dangling refs.
While no one overhear you quickly tell me not cow cow.
but how about watch phone?
User avatar
phlip
Restorer of Worlds
 
Posts: 6731
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia

Re: The "IT DOESN'T WORK!" thread

Postby tipo test » Tue Mar 08, 2011 11:08 am UTC

phlip wrote:
Code: Select all
select person from eats group by person having count(distinct fruit) = (select count(id) from fruit);


Awesome, I understand this and it works! Well if you create properly the tables with primary key constraints there shouldn't be the problem you mention no?

About the other way, I don't get it and in any case you're using the value 1 directly aren't you?

Thanks a lot phlip.
User avatar
tipo test
 
Posts: 71
Joined: Sun May 23, 2010 12:15 pm UTC

Re: The "IT DOESN'T WORK!" thread

Postby phlip » Tue Mar 08, 2011 11:59 pm UTC

The actual select list doesn't matter in an EXISTS() or NOT EXISTS() subquery... only whether or not the query brings out any rows. So it's idiomatic to do something like "SELECT 1 FROM etc" to indicate that the values aren't being used. You could also do "SELECT *" or "SELECT id" or whatever, but it wouldn't make any difference.

"WHERE EXISTS(SELECT anything FROM etc)" is the same as "WHERE (SELECT COUNT(*) FROM etc) > 0", except it knows it can stop running the subquery as soon as it finds a single matching row... it doesn't need to count all of them.
While no one overhear you quickly tell me not cow cow.
but how about watch phone?
User avatar
phlip
Restorer of Worlds
 
Posts: 6731
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia

Re: The "IT DOESN'T WORK!" thread

Postby Kain » Thu Mar 10, 2011 12:20 am UTC

So, I was trying to get a particular class file to run in TeXWorks for a school project that required us to write it in LaTeX, and through a rather convoluted process actually managed to kill one computer and delete the program from the other. In any case, I decided I would replace TeXWorks with TeXlipse, so that I would actually have a working spell checker, etc. The problem is that, while I can get eclipse (helios) to run, and can get eclipse to find the TeXlipse source website, when I go to actually install it I am informed that
Spoiler:
Cannot complete the install because one or more required items could not be found.
Software currently installed: Eclipse SDK 3.6.2.M20110210-1200 (org.eclipse.sdk.ide 3.6.2.M20110210-1200)
Missing requirement for filter properties ~= $0: Eclipse Product Configuration 1.0.0.M20110210-1200 (org.eclipse.rcp.configuration.feature.group 1.0.0.M20110210-1200) requires 'org.eclipse.rcp.configuration_root.win32.win32.x86_64 [1.0.0.M20110210-1200]' but it could not be found
Cannot satisfy dependency:
From: Eclipse SDK 3.6.2.M20110210-1200 (org.eclipse.sdk.ide 3.6.2.M20110210-1200)
To: org.eclipse.rcp.configuration.feature.group [1.0.0.M20110210-1200]
. I am honestly at a loss for what that is trying to tell me...
Look, you know it's serious when a bunch of people in full armor and gear come charging in to fight a pond of chickens - Steax
Kain
 
Posts: 1116
Joined: Wed Aug 27, 2008 4:29 am UTC
Location: At the center of the observable universe.

Re: The "IT DOESN'T WORK!" thread

Postby TheChewanater » Fri Mar 11, 2011 9:42 pm UTC

Code: Select all
template <typename T>
class RotationMatrix
{
    T a, b, c, d, e, f, g, h, i;
    
    public
:
        RotationMatrix (const vector3d<T>& eulers)
        {
            T x = eulers.X;
            T y = eulers.Y;
            T z = eulers.Z;
            
            a 
= cos(x) * cos(z);
            b = -cos(y) * sin(z) + sin(y) * sin(x) * cos(z);
            c = sin(y) * sin(z) + cos(y) * sin(x) * cos(z);
            d = cos(x) * sin(z);
            e = cos(y) * cos(z) + sin(y) * sin(x) * sin(z);
            f = -sin(y) * cos(z) + cos(y) * sin(x) *sin(z);
            g = -sin(x);
            h = sin(y) * cos(x);
            i = cos(y) * cos(x);
        }
        
        vector3d
<T> operator* (T xyz[3])
        {
            T x0 = xyz[0];
            T y0 = xyz[1];
            T z0 = xyz[2];
            
            T x 
= a * x0 + b * y0 + c * z0;
            T y = d * x0 + e * y0 + c * z0;
            T z = g * x0 + e * y0 + c * z0;
            
            return 
{x, y, z};
        }
};
 

Code: Select all
float xyz[] = {rotation.* M_PI / 180, 
        rotation
.* M_PI / 180,
        rotation.* M_PI / 180};
    
    RotationMatrix
<float> r (position);
    position += r * xyz; 


I'm trying to make a flight simulator-ish thing. I'd think that this code should make the ship move foward based on its rotation, but it doesn't. Did I make a trivial mistake somewhere, or is there something about transformation matrices that I misunderstand?
ImageImage
http://internetometer.com/give/4279
No one can agree how to count how many types of people there are. You could ask two people and get 10 different answers.
User avatar
TheChewanater
 
Posts: 1260
Joined: Sat Aug 08, 2009 5:24 am UTC

Re: The "IT DOESN'T WORK!" thread

Postby squareroot » Sat Mar 12, 2011 2:09 am UTC

You're taking sin(position.X), Y, and Z? That... doesn't seem right... do you mean to be taking the sin/cos of some angles, like yaw and pitch?
<signature content="" style="tag:html;" overused meta />
squareroot
 
Posts: 543
Joined: Tue Jan 12, 2010 1:04 am UTC

Re: The "IT DOESN'T WORK!" thread

Postby _Axle_ » Sat Mar 12, 2011 3:37 am UTC

@TheChewanater . .

First, Rotation Matrix really doesn't need to be a template. I can't think of any types that would work other than float or doubles. Especially for a game like setting.

Second, You need the original 'facing' of the object, which will be a vector. This vector then needs to be multiplied by the rotation matrix of the correct angles. This will give the new heading direction. Probably want to normalize that new direction and then multiply that by the current speed. This will give how far along that direction, and then you add that to your original position, this will move it ( non-forced based physics movement ).

From your second code sample, you seem to be building a rotation matrix from your current position, shouldn't that be built from xyz or whatever your current angles are? I can't really double check what your rotation matrix is, since usually for games, it is broken into 3 different matrices ( rotation around X, Y and Z ) separately and then just concatenated together before rotating the vector.
Yakk wrote:Computer Science is to Programming as Materials Physics is to Structural Engineering.
_Axle_
 
Posts: 253
Joined: Fri Sep 24, 2010 7:33 pm UTC

Re: The "IT DOESN'T WORK!" thread

Postby Yakk » Sat Mar 12, 2011 4:17 am UTC

It being a template isn't a horrible idea. You can swap between float and double, or even fixed point.

Building up your rotation matrix from components is a good idea. You only have to compute it once per frame/time slice, which is basically never at modern computer speeds.

Next, note that your code is doing a full matrix multiplication anyhow. So just write a matrix class.

Code: Select all
// does work for us:
template<int n, typename T>
struct Vector_base
{
  T values[n];
  T& operator[](int i){
    Assert(i >= 0);
    Assert(i < n);
    return values[i];
  }
  T const& operator[](int i) const{
    Assert(i >= 0);
    Assert(i < n);
    return values[i];
  }
  Vector_base() {
    for(int i = 0; i < n; ++i) {
      values[i] = T();
    }
  }
};

template<int n=3, typename T=double>
struct Vector: public Vector_base<n, T>
{};

template<typename T>
struct Vector<3, T>: public Vector_base<n, T>
{
  T& x() { return (*this)[0]; }
  T const& x() const { return (*this)[0]; }
  T& y() { return (*this)[1]; }
  T const& y() const { return (*this)[1]; }
  T& z() { return (*this)[2]; }
  T const& z() const { return (*this)[2]; }
};

A really stripped down vector class. I'd also implement +=, and maybe use the CRTP (google it) to have the Vector_base return Vector types from operators like +=.

But I'm a bit of a template geek. In practice, Vector3 is what you usually want.

Then we toss together Matrix:
Code: Select all
template<int n=3, typename T=double>
struct Matrix
{
  struct slice {
    int i;
    Matrix* m;
    slice(int i_, Matrix<n, T>* m_):i(i_), m(m_) {}
    T& operator[]( int j ) const { Assert(j >=0); Assert(j<n); return (*m).value[i][j]; }
  };
  struct const_slice {
    int i;
    Matrix const* m;
    const_slice(int i_, Matrix<n, T> const* m_):i(i_), m(m_) {}
    T const& operator[]( int j ) const { Assert(j >=0); Assert(j<n); return (*m).value[i][j]; }
  };
  T values[n][n];
  static void zero( T& t ) { t = T(); }
  void ForEachElement( std::tr1::function< void(T&) > func ) {
    for(int i = 0; i < n; ++i){
      for(int j=0; j<n;++j){
        func(value[i][j]);
      } 
    }
  }

  Matrix()
  {
    this->ForEachElement(Matrix::zero);
  }
  slice operator[](int i){ Assert(i>=0); Assert(i<n); return slice(i, this); }
  const_slice operator[](int i) const { Assert(i>=0); Assert(i<n); return const_slice(i, this); }
};

which, for the hell of it, has bounds-checking (in debug) operator[][] implemented.

Then we write:
Code: Select all
template<int n, typename T>
Vector<n,T> operator*( Matrix<n, T> const& left, Vector<n, T> const& right )
{
  Vector<n, T> retval;
  for( int i = 0; i < n; ++i )
  {
    for( int j = 0; j < n; ++j )
    {
      retval[i] += left[i][j]*right[j];
    }
  }
  return retval;
}

which gives you Matrix times Vector multiplication, and
Code: Select all
template<int n, typename T>
Matrix<n, T> operator*( Matrix<n, T> const& left, Matrix<n, T> const& right )
{
  Matrix<n, T> retval;
  for( int i = 0; i < n; ++i )
  {
    for( int j = 0; j < n; ++j )
    {
      for( int k = 0; k < n; ++k )
      {
        retval[i][j] += left[i][k]*right[k][j];
      }
    }
  }
  return retval; 
}


Of course, you could have more fun and write Matrixes as Vectors of Vectors, and teach operator* to deal with non-uniform Vector multiplication in such a way that matrix multiplication "falls out". But that is getting silly (and requires a strange default orientation for Matrixes, so probably isn't worth it). (note that any decent compiler will take the above loops and flatten them).

Anyhow, once you have the above, you can just write a few Matrix primitive generators.

Have one "rotate clockwise around z axis". Then a "rotate z axis to y axis" and "rotate z axis to x axis".

From those primitives, you can write "rotate clockwise around x axis" and "rotate clockwise around y axis" using the above matrix operator*.

Now you can generate the arbitrary rotation matrix by simply multiplying the above matrices together.

And why do it this way? Because you can test the pieces without testing the output. You can examine the "rotate around z" matrix, and manually test if it does what you want. The same for the pieces.

Then you can manually test the operator* (which may or may not be right -- the one I wrote above was off the top of my head, it may not even compile).

Once each piece is tested, you can run them all together and have confidence that the result is right.

If you just toss them all together at once, about the only thing you can do is hope that wherever you copied it from was accurate.
One of the painful things about our time is that those who feel certainty are stupid, and those with any imagination and understanding are filled with doubt and indecision - BR

Last edited by JHVH on Fri Oct 23, 4004 BCE 6:17 pm, edited 6 times in total.
User avatar
Yakk
 
Posts: 10036
Joined: Sat Jan 27, 2007 7:27 pm UTC
Location: E pur si muove

Re: The "IT DOESN'T WORK!" thread

Postby Ptolom » Tue Mar 15, 2011 6:36 pm UTC

I'm trying to get ptrace to tell me when a program segfaults but all I can get out of it is the SIGTRAP ptrace sends itself. Does anybody know anything about how signals are supposed to work with ptrace?
I have
Code: Select all
char** subjargv=argv+1;
  pid_t subjpid;
  subjpid= fork();
  struct user_regs_struct regs;
  unsigned long buffstart=0;
  siginfo_t subjsigs;

  if (subjpid==0){
    ptrace(PTRACE_TRACEME, 0,NULL,NULL);
    sleep(1);
    if(execv (subjargv[0] , subjargv)<0){
      fprintf(stderr,"Cannot Launch %s\n",argv[1]);
      return (1);
    }
  }
  else if(subjpid<0){
    fprintf(stderr,"failed to fork\n");
    return (1);
  }

  wait(NULL);
  ptrace(PTRACE_GETSIGINFO,subjpid,NULL,&subjsigs);
  printf("sig=%d\n",subjsigs.si_signo);

But it always gives me signal 5.
It Should Be Real wrote:Fuck the wizard.
We're doing this manually.

http://www.hexifact.co.uk - Hacking blog: in which I take some things apart, and put other things together.
User avatar
Ptolom
 
Posts: 1428
Joined: Mon Mar 24, 2008 1:55 pm UTC
Location: The entropy pool (Leicester)

Re: The "IT DOESN'T WORK!" thread

Postby Ptolom » Tue Mar 15, 2011 8:57 pm UTC

Ah, I sussed it. You have to tell the program to continue to get to the SIGSEGV. Solutions always come to me shortly after posting here in desperation.
It Should Be Real wrote:Fuck the wizard.
We're doing this manually.

http://www.hexifact.co.uk - Hacking blog: in which I take some things apart, and put other things together.
User avatar
Ptolom
 
Posts: 1428
Joined: Mon Mar 24, 2008 1:55 pm UTC
Location: The entropy pool (Leicester)

Re: The "IT DOESN'T WORK!" thread

Postby sourmìlk » Thu Mar 17, 2011 1:41 am UTC

So, I'm reading a bitmap file into an array of bytes using C++, but I keep getting absurdly high amounts for BITMAPINFOHEADER.biWidth and BITMAPINFOHEADER.biHeight.

First, here's how I define BYTE, WORD, and DWORD:
Code: Select all

typedef unsigned char BYTE
;
typedef unsigned short WORD;
typedef unsigned int DWORD;
 


and here's how I define the BITMAPINFOHEADER and BITMAPFILEHEADER structs:
Code: Select all

typedef struct
{
    WORD bfType;
    DWORD bfSize;
    WORD bfReserved1;
    WORD bfReserved2;
    DWORD bfOffBits;
}
 BITMAPFILEHEADER;

typedef struct
{
    DWORD biSize;
    long biWidth;
    long biHeight;
    WORD biPlanes;
    WORD biBitCount;
    DWORD biCompression;
    DWORD biSizeImage;
    long biXPelsPerMeter;
    long biYPelsPerMeter;
    DWORD biClrUsed;
    DWORD biClrImportant;
}
 BITMAPINFOHEADER;
 


And here's the code I use to read the data into the BITMAPINFOHEADER struct
Code: Select all

FILE 
*filePtr;
BITMAPFILEHEADERbmfh;
BITMAPINFOHEADER bmih;

filePtr = fopen(filePath, "rb");

fread(&bmfh, sizeof(BITMAPFILEHEADER), 1, filePtr);
fread(&bmih, sizeof(BITMAPINFOHEADER), 1, filePtr);
 


And here's the BMP file.
Terry Pratchett wrote:The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.
User avatar
sourmìlk
If I can't complain, can I at least express my fear?
 
Posts: 6405
Joined: Mon Dec 22, 2008 10:53 pm UTC
Location: permanently in the wrong

Re: The "IT DOESN'T WORK!" thread

Postby phlip » Thu Mar 17, 2011 1:51 am UTC

Have you turned off any structure padding the compiler may be doing? Check the sizeof() values... are they correct? (They should be 14 and 40.)

Many compilers will pad structure fields shorter than 32 bits up to that amount, so that all the values are word-aligned (for speed). Which will make them not match the layout in the file.

What compiler are you using? On what system?

If you're using GCC (or MSVC, but if you were on Windows you'd just use the structures in windows.h) you want this:
Code: Select all
#pragma pack(push,1)
// struct definitions here
#pragma pack(pop)
While no one overhear you quickly tell me not cow cow.
but how about watch phone?
User avatar
phlip
Restorer of Worlds
 
Posts: 6731
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia

Re: The "IT DOESN'T WORK!" thread

Postby _Axle_ » Thu Mar 17, 2011 2:05 am UTC

What Phlip said.

I checked your header size in both VS2008 and GCC, both are coming up with 16 byte size for your BITMAPFILEHEADER. The other is correct at 40 bytes.
Yakk wrote:Computer Science is to Programming as Materials Physics is to Structural Engineering.
_Axle_
 
Posts: 253
Joined: Fri Sep 24, 2010 7:33 pm UTC

Re: The "IT DOESN'T WORK!" thread

Postby sourmìlk » Thu Mar 17, 2011 2:09 am UTC

_Axle_ wrote:What Phlip said.

I checked your header size in both VS2008 and GCC, both are coming up with 16 byte size for your BITMAPFILEHEADER. The other is correct at 40 bytes.


This is probably it.

If you're using GCC (or MSVC, but if you were on Windows you'd just use the structures in windows.h) you want this:


I would rather commit seppuku than use MSVC.

Actually, when I don't use the #pragma preprocessor commands, I'm getting values of 16 and 64.. and when I do, I get 14 (which is correct), and 56 (which isn't). When I don't use them, I'm getting 16 and 64.

At least now I know it's the header sizes.
Terry Pratchett wrote:The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.
User avatar
sourmìlk
If I can't complain, can I at least express my fear?
 
Posts: 6405
Joined: Mon Dec 22, 2008 10:53 pm UTC
Location: permanently in the wrong

Re: The "IT DOESN'T WORK!" thread

Postby bittyx » Thu Mar 17, 2011 2:19 am UTC

Are you compiling on a 64-bit Unix system? Because this table shows that a C/C++ long is 8 bytes on that platform, instead of the expected 4 (which exactly explains the discrepancy).

EDIT (ninja'd): To solve this problem, you could use something like uint32_t from <stdint.h> instead of long. The types defined there are exactly the specified number of bits wide, so it's a good way to make sure your program works as expected wherever you compile it.
Last edited by bittyx on Thu Mar 17, 2011 2:27 am UTC, edited 1 time in total.
bittyx
 
Posts: 161
Joined: Tue Sep 25, 2007 9:10 pm UTC
Location: Belgrade, Serbia

Re: The "IT DOESN'T WORK!" thread

Postby phlip » Thu Mar 17, 2011 2:21 am UTC

Ah, that's a good point. You should be using the stdint.h types for something like this anyway.
While no one overhear you quickly tell me not cow cow.
but how about watch phone?
User avatar
phlip
Restorer of Worlds
 
Posts: 6731
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia

Re: The "IT DOESN'T WORK!" thread

Postby sourmìlk » Thu Mar 17, 2011 2:25 am UTC

bittyx wrote:Are you compiling on a 64-bit Unix system? Because this table shows that a C/C++ long is 8 bytes on that platform, instead of the expected 4 (which exactly explains the discrepancy).


ahah! Yes, that is exactly it. I didn't know about <stdint.h>, so I'll just include that and use those types. Thank you all.
Terry Pratchett wrote:The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.
User avatar
sourmìlk
If I can't complain, can I at least express my fear?
 
Posts: 6405
Joined: Mon Dec 22, 2008 10:53 pm UTC
Location: permanently in the wrong

PreviousNext

Return to Coding

Who is online

Users browsing this forum: No registered users and 9 guests