Was Hans Christian Andersen really as pure - and boring - as biographers make out? Director Robert Lepage thought so, until he discovered the fairytale writer's diary entries, and his perverse side

Every time Hans Christian Andersen had a wank, he would put a mark in his diary. "Today I had a visit from such-and-such a person, they're so sweet," he would write. "When they left, I had a double-sensuous ++."

In Denmark, Andersen is regarded as a national hero with a whiter-than-white image. His fans argue that the reason he never married or had sex was his desire to remain pure. Most biographies about him are very boring. But there's one, Hans Christian Andersen: The Life of a Storyteller by Jackie Wullschlager, that is fantastic. Wullschlager approaches aspects of his life that have never been discussed frankly and openly - not only sexuality but other shady sides to his character.

When he visited Paris, for example, he would go to brothels in the Porte Saint-Denis area, not to touch the women, but to speak to them, return to his hotel and wank off. Then he would write about it in his diary.

Although he didn't have an active sexual life, there's some evidence that he was bisexual, having had crushes on both men and women. In the Romantic era men would write passionate letters to each other, yet it didn't mean they wanted to sleep together; Andersen's romanticism, though, went over the top and he wrote open love letters to a lot of young men. He also had great passions for a few women, although they were women he was pretty sure it would be impossible to love - Jenny Lind, for example, one of the great Swedish sopranos, whose touring schedule made a relationship out of the question.

It was discovering that this man best known for writing children's stories had a double life, a strange, troubled personal history, that made me agree to do a show about him. I was first approached by the organisers of HCA 2005, Denmark's celebration of the storyteller's 200th birthday, with the idea in 2000, but it wasn't until I read Wullschlager's biography in 2003 that I accepted the commission. And my first idea for The Andersen Project was to do with masturbation.

The theme came about not in a sleazy, crass way, but as a way of trying to understand Andersen. I don't want to shock - I just want to show Andersen's lucid vision of the human condition. And the theme makes extra sense because a solo show is the most solitary form of performance and masturbation is the most solitary form of sex.

But what would HCA 2005 say? When I told to Lars Seeberg, the artistic advisor of HCA 2005, my take on the Andersen story, his response was: "It sounds fantastic. Now I have to go and see Her Majesty the Queen [Margrethe II] and explain that you're going to do a show about Hans Christian Andersen masturbating." I later met the queen myself; we didn't discuss it, but she was very sweet and a cultivated woman, so it probably went down very well.

It's hard to talk about what Andersen and I have in common without sounding pretentious, but there's a lot about him that I identify with - not least his insatiable sexual desire and constant mood of sensuousness. The difference between us is that I have a very intense sexual life and he never did. There is a connection between sexuality and creativity, and one of the themes in The Andersen Project is to do with the imaginative and sexual development of children. Reading fairytales to children expands their imaginations. As they grow older, they replace their bedtime stories with masturbation and sexual fantasy. I always worried that I was a sex maniac because I thought about sex all the time, but actually it's part of the imaginative process. If you're a storyteller and spend your time imagining things, your sexual imagination is likely to be just as vivid.

Perhaps Andersen's sexual uncertainty reflects his difficult childhood. It's no coincidence that it was Andersen who wrote The Ugly Duckling, a metaphor for the awkwardness of childhood and the blossoming of adulthood. I can identify with this, too: where Andersen was tall and ungainly, I had alopecia. Both of us experienced how cruel children can be. That can be tough, but being put through the mill very young can also be an advantage because you don't see the world in the same way.

Another thing that connects us is the need to travel. A lot of artists in the 19th century felt that they had to travel outside their own country to be recognised. But Andersen felt he had more reason than most. First, he wrote in Danish, a language that, for a lot of people in Europe, was like speaking backwards. Second, he wrote for children, so he wasn't taken seriously. To be recognised, he had to go Germany and France to mingle among the great writers of the day. He'd come back to Denmark with all of that recognition.

If you are a Quebecois artist, as I am, you feel the same impulse. Even an English-Canadian feels he has to be approved by London, Paris or New York. But Andersen sometimes did things for the wrong reasons - just like the heroes in his stories. I echo this in The Andersen Project, in which the main character is a Canadian rock lyricist who goes to Paris just to be approved and realises he's not doing it for the right reason.

The Andersen Project is inspired by two of the later tales. After hitting 60, Andersen wrote some of his best stories, aimed more at adults than children and much more experimental than his earlier, better-known work. One of them is The Dryad: it takes place in Paris during the World Exhibition of 1867, which Andersen visited twice, having been captivated by the mechanical inventions on display. The story, about a tree spirit who longs for the excitement of the city, reflects Europe's abandonment of romanticism in favour of modernism, something that fascinated Andersen.

The other inspiration is The Shadow, which is his most interesting fairytale, one people rarely read to their children because it's so scary. Like Freud and Jung before their time, it suggests that each human being has a shadowy part that will, if you let it, destroy you. This is reflected in one of my characters, the manager of a Paris opera house, who has an addiction to pornography that eventually eats him up. When I performed the show in Paris in November, I was afraid that this character, and my jokes about the French always going on strike, would offend people - not least the people who had helped to fund the show. It didn't help that The Andersen Project also satirises the world of international co-productions - the very people I collaborate with. Luckily, they are as self-critical as they are critical and the performances were great fun.

But to be offensive is not using rude words or being critical of a culture. What's offensive is when you haven't done your homework and haven't tried to understand what a culture is about. If you haven't done that part, you have no right to make fun of it.

That's what's so offensive about a lot that we see: people have the pretension to do a big production of Chekhov, for example, without having an idea of what Russia's about. They change the period of the piece because they want to make a political statement. I find that more offensive than doing a show about a great Danish icon masturbating.

· The Andersen Project opens at the Barbican, London EC2, on January 26. Box office: 020-7638 8891

Andersen's darkest stories

The Little Mermaid

Highly disturbing and morbid tale about a mermaid who makes a diabolical bargain with a sea witch and suffers her tongue to be cut out and her tail to be lost, all for the love of a prince. Inevitably, he completely fails to recognise the enormity of her sacrifice.

The Red Shoes

Vanity is the sin of the anti-heroine (vindictively named Karen after Andersen's loathed half-sister) in this nasty tale (made into a film in 1948). Karen's sin of going to church in bright red shoes and failing to care for her grandmother is punished by her being forced to dance unceasingly for ever. Rest comes only when her feet are cut off with an axe.

The Ice Maiden

There is a touch of the earlier Snow Queen in this novella, a dark, tragic love story about Rudy and Babette who are stolen away by the icy Glacier Queen, a terrifying figure who represents death.

Poultry Meg's Family

One of several of Andersen's highly charged and erotic tales. The sexually voracious heroine - inspired by a Danish historical figure - swaps a comfortable life with her rich husband for a bit of "rough trade" and lovers who beat her.

Anne Lisbeth

Spooky story telling the fate suffered by a woman who rejects her ugly son and becomes a nursemaid to the count's son instead. Retribution comes in the form of rejection by those she has served, the drowning of her own son and guilt-ridden nightmares.

Lyn Gardner