If I am understanding the problem here, @ThisSuitIsBlackNot++ has written a function (x_intercepts
which is available in the thread: Function to extract intersections of crossing lines on axes) that expects its argument to be a reference to a list of array references. The x_intercepts
subroutine in turn uses a function from Math::Geometry::Planar
which expects the points of a line segment to be passed as series of array references/anonymous arrays that contain the x,y
values for each point.
Again - it is not entirely clear - but it seems your data is in two different arrays: one containing all the x values and one with the corresponding y values. Is this the case? If this is not correct please leave a comment and I will remove this answer.
If that is the source of your problem then you can "munge" or transform your data before you pass it to x_intercepts
or - as @ThisSuitIsBlackNot suggests - you can rewrite the function. Here is an example of munging your existing data into an "@input_list
" to pass to x_intercepts
.
my @xs = qw/-1 1 3/;
my @ys = qw/-1 1 -1 /;
my @input_list ;
foreach my $i ( 0..$#ys ) {
push @input_list, [ $xs[$i], $ys[$i] ] ;
}
my $intercept_list = x_intercepts(@input_list) ;
say join ",", @$_ for @$intercept_list ;
Adding the lines above to your script produces:
Output:
0,0
2,0
You have to be very careful doing this kind of thing and using tests to make sure you are passing the correctly transformed data in an expected way is a good idea.
I think a more general difficulty is that until you are familiar with perl it is sometimes tricky to easily see what sorts of values a subroutine is expecting, where they end up after they are passed in, and how to access them.
A solid grasp of perl data structures can help with that - for example I think what you are calling a "double array" or "double element" here is an "array of arrays". There are ways to make it easier to see where default arguments passed to a subroutine (in @_
) are going (notice how @ThisSuitIsBlackNot has passed them to a nicely named array reference: "($points)
"). Copious re-reading of perldoc
perbsub
can help things seem more obvious.
References are key to understanding perl subroutines since to pass an array or hash to a subrouting you need to do so by references. If the argument passed x_intercepts
is a list of two lists of anonymous arrays then when it is assigned to ($points)
, @$points->[0]
@$points->[1]
will be the arrays contain those lists.
I hope this helps and is not too basic (or incorrect). If @ThisSuitIsBlackNot finds the time to provide an answer you should accept it: some very useful examples have been provided.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…