POV-Ray : Newsgroups : povray.general : Convert strings to arrays? Server Time
2 Nov 2024 05:21:53 EDT (-0400)
  Convert strings to arrays? (Message 1 to 6 of 6)  
From: SharkD
Subject: Convert strings to arrays?
Date: 28 Oct 2009 01:28:26
Message: <4ae7d67a@news.povray.org>
Is there a function I can use to convert a string into an array?

Thanks!

Mike


Post a reply to this message

From: nemesis
Subject: Re: Convert strings to arrays?
Date: 28 Oct 2009 02:15:00
Message: <web.4ae7e1404930f680f2ed51020@news.povray.org>
SharkD <mik### [at] gmailcom> wrote:
> Is there a function I can use to convert a string into an array?

I once saw a trick in which some pov code wrote the string to a file and then
#include'd it back.  Might be a start.


Post a reply to this message

From: Chris B
Subject: Re: Convert strings to arrays?
Date: 28 Oct 2009 07:21:31
Message: <4ae8293b$1@news.povray.org>
"SharkD" <mik### [at] gmailcom> wrote in message 
news:4ae7d67a@news.povray.org...
> Is there a function I can use to convert a string into an array?
>
> Thanks!
>
> Mike

I don't remember seeing a standard function, but it's quick to split a 
string with the existing POVRay functions and a #while loop (see the 2 
examples below).

If you want more sophisticated processing you'll find some macros in the 
Scroll include file from the Object Collection that can left, right, centre 
or full justify text strings.

Note that the examples below write the strings to the message stream and 
produce only a completely black image.

Regards,
Chris B.


camera { location z look_at 0 }
light_source { <-5,30,-10> 1 }

#declare Str = "How many roads must a man walk down?";

// Convert string to array of single characters
#declare Arr = array[strlen(Str)];
#declare Ctr = 0;
#while (Ctr<strlen(Str))
  #declare Arr[Ctr] = substr(Str,Ctr+1,1);
  #debug concat(Arr[Ctr],"\n")
  #declare Ctr = Ctr+1;
#end

// Split string on spaces
#declare Arr = array[strlen(Str)];
#declare Ctr = 0;
#declare Pos = 1;
#declare Words = 0;
#while (Ctr<strlen(Str))
  #if (strcmp(substr(Str,Ctr+1,1)," ")=0)
    #declare Arr[Words] = substr(Str,Pos,Ctr-Pos+1)
    #declare Pos = Ctr+2;
    #debug concat(Arr[Words],"\n")
    #declare Words = Words+1;
  #end
  #declare Ctr = Ctr+1;
#end
#declare Arr[Words] = substr(Str,Pos,strlen(Str)-Pos+1)
#debug concat(Arr[Words],"\n")
#declare Words = Words+1;


Post a reply to this message

From: SharkD
Subject: Re: Convert strings to arrays?
Date: 28 Oct 2009 22:26:46
Message: <4ae8fd66$1@news.povray.org>
On 10/28/2009 7:21 AM, Chris B wrote:
> I don't remember seeing a standard function, but it's quick to split a
> string with the existing POVRay functions and a #while loop (see the 2
> examples below).
>
> If you want more sophisticated processing you'll find some macros in the
> Scroll include file from the Object Collection that can left, right,
> centre or full justify text strings.
>
> Note that the examples below write the strings to the message stream and
> produce only a completely black image.
>
> Regards,
> Chris B.

Nice tip, thanks!

Next question: How do I modify a one character text object such that the 
character fits inside a 1x1x1 cube? (Note that I am working only with 
single character strings, so I don't have to worry about what happens 
with larger strings.)

Mike


Post a reply to this message

From: Chris B
Subject: Re: Convert strings to arrays?
Date: 29 Oct 2009 04:39:07
Message: <4ae954ab$1@news.povray.org>
"SharkD" <mik### [at] gmailcom> wrote in message 
news:4ae8fd66$1@news.povray.org...
>
> Next question: How do I modify a one character text object such that the 
> character fits inside a 1x1x1 cube? (Note that I am working only with 
> single character strings, so I don't have to worry about what happens with 
> larger strings.)
>

Assign a text object to an identifier, then use the min_extent and 
max_extent functions to find the dimensions of the resulting object and 
scale it accordingly as you add it to the scene:


#declare Letter = text {ttf "arial.ttf","~",1,0}

object {Letter
  translate -min_extent(Letter)
  scale 1/(max_extent(Letter)-min_extent(Letter))
  pigment {rgb <1,1,0>}
}

box {-0.001,1.001 pigment {rgbt 0.7}}


Though you don't need it to, this should work the same with a 
multi-character string and should work reasonably well with a lot of other 
POV-Ray objects to boot. These functions actually measure the bounding box 
of the object, but I think that the bounding box aligns pretty well with the 
actual extents of a text object (I didn't yet notice any big differences). 
It doesn't align very well with certain CSG objects and I hate to think what 
would happen with infinite objects such as planes :-)

Regards,
Chris B.


Post a reply to this message

From: SharkD
Subject: Re: Convert strings to arrays?
Date: 29 Oct 2009 23:57:15
Message: <4aea641b@news.povray.org>
On 10/29/2009 4:39 AM, Chris B wrote:
> Assign a text object to an identifier, then use the min_extent and
> max_extent functions to find the dimensions of the resulting object and
> scale it accordingly as you add it to the scene:

Again, that worked perfectly. Much thanks!

Mike


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.