10
\$\begingroup\$

Given two numbers, print a page with an "X" in the specified location (±10%).

The numbers must be either percentages or fractions of the page's print area dimensions, e.g. [25,75] or ["15%","0%"] or [0.33,1]. You do not have to consider margins. Just remember to state which number is width, and which is height, and which corner is the origin.

You may of course assume that a printer is connected, and any virtual printer counts too. In fact, you may want to set a PDF printer as your default printer before beginning this challenge.

You may create a printable file (e.g. a PDF) and send that to a virtual printer, which in turn creates your result PDF, but you may not directly create the final PDF. This has to be a proper print job.

If possible, please include a photo, scan, or screenshot of the result, or link to an online document. Note that Imgur accepts PDFs.

You will of course get more upvotes if you actually place an X rather than outputting newlines and spaces with an assumed font and paper size.

Example

For input height 75% and width 25%, from top left, print the following document:


[25,75]


\$\endgroup\$
  • \$\begingroup\$ Are we allowed to read the image file from anywhere? \$\endgroup\$ – Beta Decay May 16 '17 at 9:38
  • \$\begingroup\$ @BetaDecay What image file? \$\endgroup\$ – Adám May 16 '17 at 9:38
  • \$\begingroup\$ The image in your question: i.stack.imgur.com/rfZeO.png \$\endgroup\$ – Beta Decay May 16 '17 at 11:00
  • \$\begingroup\$ @BetaDecay You are supposed to generate a document, not read an image. \$\endgroup\$ – Adám May 16 '17 at 11:01
  • \$\begingroup\$ What paper size should we use/assume, or does it matter? \$\endgroup\$ – Stephen May 16 '17 at 14:26
6
\$\begingroup\$

MacOS Bash + OfficeJet Pro 8600, 46

(yes ''|sed $[$2*3/5]q;printf %$[$1*4/5]sX)|lp

Origin is the top-left corner.

I did a test print to check the character dimensions of what gets printed by lp to my printer - it appears to be 82w x 64h, so (integer) multiplying percentages by 3/5 and 4/5 respectively gets within the 10% tolerance.

Result, with command-line input of 25 75:

enter image description here

\$\endgroup\$
1
\$\begingroup\$

APL (Dyalog) on Windows, 36 35 bytes

Anonymous function taking (Y,X) in percent from top left as argument

{'X.'⎕WC'Text'(2↓'X'⎕WC'Printer')⍵}

We begin with a list of three elements:

{ an anonymous function (all variables are local)

 the argument, e.g. 75 25

( to the right of

  'X'⎕WC'Printer'Windows Create a Printer object named X, returning '#.X'

  2↓ drop two characters to get just 'X'

) to the right of

'Text' what we will add

'X.'⎕WCWindows Create a (type, content, position) object in the object X, returning '#.X'

} end of function (since X is local, it is destroyed when the function terminates, signalling to Windows that we are done with the print job, which causes Windows to print it)


X @ 75 25

\$\endgroup\$
0
\$\begingroup\$

Java 8, 210 209 bytes

-1 byte: changed to currying syntax

import java.awt.print.*;
x->y->{PrinterJob j=PrinterJob.getPrinterJob();j.setPrintable((g,p,i)->{g.drawString("X",(int)(p.getWidth()*x),(int)(p.getHeight()*y));return i;});try{j.print();}catch(Exception e){}};

Takes input as floating-point version of percent: 0.25, 0.75.

If paper size can be assumed as 8.5x11in (72dpi), 184 bytes

import java.awt.print.*;
x->y->{PrinterJob j=PrinterJob.getPrinterJob();j.setPrintable((g,p,i)->{g.drawString("X",x*612/100,y*792/100);return i;});try{j.print();}catch(Exception e){}};

Uses default values for printing to PDF. Now takes input as integer version of percent: 25, 75.

If a Windows OS can also be assumed, 177 bytes

import sun.awt.windows.*;
x->y->{WPrinterJob j=new WPrinterJob();j.setPrintable((g,p,i)->{g.drawString("X",x*612/100,y*792/100);return i;});try{j.print();}catch(Exception e){}};

Same inputs as above: 25, 75.

Usage

import java.awt.print.*;
...
Function<Double, Consumer<Double>> f =
x->y->{PrinterJob j=PrinterJob.getPrinterJob();j.setPrintable((g,p,i)->{g.drawString("X",(int)(p.getWidth()*x),(int)(p.getHeight()*y));return i;});try{j.print();}catch(Exception e){}};
...
f.apply(.25).accept(.75);

Test Case

For input of 0.25, 0.75:

Java Printing Test Case

(I don't have a physical printer, but this should still work without issue)

\$\endgroup\$
  • \$\begingroup\$ Very impressive. Why do you need catch(Exception e){}? \$\endgroup\$ – Adám May 23 '17 at 4:43
  • \$\begingroup\$ @Adám Because PrinterJob.print() throws a PrinterException and I didn't see any way to get around it. \$\endgroup\$ – Justin Mariner May 23 '17 at 10:19
0
\$\begingroup\$

C#, 259 202 201 bytes

namespace System.Drawing.Printing{w=>h=>{var p=new PrintDocument();p.PrintPage+=(s,e)=>e.Graphics.DrawString("X",new Font("Onyx",9),Brushes.Red,e.PageBounds.Width*w,e.PageBounds.Height*h);p.Print();};}

Onyx is a font installed on my Windows 10 machine by default where 4 seems to be the shortest font name. I tested that by running the following in C# and then inspecting blah in the debugger.

var blah = FontFamily.Families.GroupBy(o => o.Name.Length)
                              .OrderBy(g => g.Key);

Full/formatted version:

namespace System.Drawing.Printing
{
    class P
    {
        static void Main(string[] args)
        {
            Func<float, Action<float>> f = w => h =>
            {
                var p = new PrintDocument();
                p.PrintPage += (s, e) =>
                    e.Graphics.DrawString("X", new Font("Onyx", 9), Brushes.Red,
                                          e.PageBounds.Width * w, e.PageBounds.Height * h);
                p.Print();
            };

            f(0.25f)(0.75f);
        }
    }
}

Test case (0.25, 0.75):


Example document

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.