Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6359453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:31:15+00:00 2026-05-24T23:31:15+00:00

I have a series of 2-D coordinates being supplied to a perl program from

  • 0

I have a series of 2-D coordinates being supplied to a perl program from another program. There are 4 of these and they make up a quad, giving 8 numbers in total, e.g:

x1 y1 x2 y2 x3 y3 x4 y4

I want to ensure that they are all specified in the same order, i.e. clockwise or counter clockwise. I already know how to do this and am doing it by looking at the sign of a cross product.

use strict;
use warnings;

my $line = "-0.702083 0.31 -0.676042 -0.323333 0.74375 -0.21 0.695833 0.485";
my @coord = split(/[,\s]+/, $line);

# Vector cross product (Z is 0) to test CW/CCW
my @v1 = (-$coord[2]+$coord[0], -$coord[3]+$coord[1]);
my @v2 = (-$coord[2]+$coord[4], -$coord[3]+$coord[5]);
my $cross = ($v1[0]*$v2[1]) - ($v1[1]*$v2[0]);

Once I’ve worked out if the order needs to be changed I currently change it using:

@coord = ($coord[6], $coord[7], $coord[4], $coord[5], 
          $coord[2], $coord[3], $coord[0], $coord[1]) if ($cross < 0);

This works, but I’m fairly sure it’s not the nicest way of writing it in perl. Is there a more elegant, “perly” way of writing this change in order? Something that would work for $n 2-D pairs preferably. It’s not a simple reverse the elements of an array problem.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T23:31:16+00:00Added an answer on May 24, 2026 at 11:31 pm

    The last couple of lines can be rewritten using an array slice:

    @coord = @coord[6,7,4,5,2,3,0,1] if $cross < 0;
    

    To process an arbitrary number of pairs, you can use List::MoreUtils::natatime

    use List::MoreUtils 'natatime';   
    
    my $it = natatime 2, @coord;
    @coord = (); 
    
    while (my @vals = $it->()) {
        unshift @coord, @vals;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a series of CodedUI test methods that make up a single test
I have a series of GPS coordinates in decimal dotted format, multiplied by 1.000.000
I have a series of maps in a Joomla-based website (there is a map
I have series of mysql databases on my server. The names of these databases
I am working in R. I have a series of coordinates in decimal degrees,
I have a series of Python tuples representing coordinates: tuples = [(1,1), (0,1), (1,0),
I have a series of urls in a web doc, something like this: <a
I have a series of buttons that display filenames. If I set the Text
I have a series of tables that contain data I want to full text
I have a series of XSL 2.0 stylesheets that feed into each other, i.e.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.