如何从索引获取行和列? [英] How to get row and column from index?
问题描述
我在这里画一个巨大空白.
我发现的所有内容都是关于从给定的行和列中获取索引,但是如何从索引中获取行和列?
Everything I've found is about getting an index from a given row and column, but how do I get a row and a column from an index?
该行很容易:(int)(index / width)
.
我的大脑正试图计算色谱柱而流血.
My brain is suffering massive bleed trying to compute the column.
对我感到羞耻.
推荐答案
对于从零开始的索引,两个操作是:
For a zero-based index, the two operations are:
row = (int)(index / width)
column = index % width
由于我是C语言人,所以我在这里使用%
,尽管您尚未指定语言,但它肯定看起来像是基于C语言的语言之一.
I'm using %
here since I'm a C guy and, though you haven't specified your language, it certainly looks like one of the C-based ones.
如果您的问题不是 与C或其兄弟有关,则对于您的特定环境而言,取模运算符将是什么,当您将index
除以width
时剩下的余数.
If your question does not pertain to C or its brethren, it will be whatever the modulo operator is for your particular environment, the remainder left over when you divide index
by width
.
如果您没有拥有模运算符,则可以使用:
If you don't have a modulo operator, you can use:
row = (int)(index / width)
column = index - (row * width)
这篇关于如何从索引获取行和列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!